diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2012-03-02 22:38:30 -0800 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2012-03-02 22:38:30 -0800 |
commit | eafa58b566c770d5d5dc5706464ab4b67e453464 (patch) | |
tree | af57a545ec7ea8a735f1eea6b70804c275b48c23 /activemodel | |
parent | 14f06dd8711231157435282cb288728c5f643303 (diff) | |
parent | 3b822e91d1a6c4eab0064989bbd07aae3a6d0d08 (diff) | |
download | rails-eafa58b566c770d5d5dc5706464ab4b67e453464.tar.gz rails-eafa58b566c770d5d5dc5706464ab4b67e453464.tar.bz2 rails-eafa58b566c770d5d5dc5706464ab4b67e453464.zip |
Merge pull request #5253 from guilleiguaran/amo-model
Add ActiveModel::Model, a mixin to make plain Ruby objects work with AP out of box
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/CHANGELOG.md | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model.rb | 1 | ||||
-rw-r--r-- | activemodel/lib/active_model/model.rb | 22 | ||||
-rw-r--r-- | activemodel/test/cases/model_test.rb | 19 |
4 files changed, 44 insertions, 0 deletions
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index a6d3afe653..42adbec8b9 100644 --- a/activemodel/CHANGELOG.md +++ b/activemodel/CHANGELOG.md @@ -1,3 +1,5 @@ +* Added ActiveModel::Model, a mixin to make Ruby objects work with AP out of box *Guillermo Iguaran* + * `AM::Errors#to_json`: support `:full_messages` parameter *Bogdan Gusiev* * Trim down Active Model API by removing `valid?` and `errors.full_messages` *José Valim* diff --git a/activemodel/lib/active_model.rb b/activemodel/lib/active_model.rb index 85514e63fd..2586147a20 100644 --- a/activemodel/lib/active_model.rb +++ b/activemodel/lib/active_model.rb @@ -39,6 +39,7 @@ module ActiveModel autoload :Errors autoload :Lint autoload :MassAssignmentSecurity + autoload :Model autoload :Name, 'active_model/naming' autoload :Naming autoload :Observer, 'active_model/observing' diff --git a/activemodel/lib/active_model/model.rb b/activemodel/lib/active_model/model.rb new file mode 100644 index 0000000000..9228d54015 --- /dev/null +++ b/activemodel/lib/active_model/model.rb @@ -0,0 +1,22 @@ +module ActiveModel + module Model + def self.included(base) + base.class_eval do + extend ActiveModel::Naming + extend ActiveModel::Translation + include ActiveModel::Validations + include ActiveModel::Conversion + end + end + + def initialize(params={}) + params.each do |attr, value| + self.send(:"#{attr}=", value) + end if params + end + + def persisted? + false + end + end +end diff --git a/activemodel/test/cases/model_test.rb b/activemodel/test/cases/model_test.rb new file mode 100644 index 0000000000..d3e00b044f --- /dev/null +++ b/activemodel/test/cases/model_test.rb @@ -0,0 +1,19 @@ +require 'cases/helper' + +class ModelTest < ActiveModel::TestCase + include ActiveModel::Lint::Tests + + class BasicModel + include ActiveModel::Model + attr_accessor :attr + end + + def setup + @model = BasicModel.new + end + + def test_initialize_with_params + object = BasicModel.new(:attr => "value") + assert_equal object.attr, "value" + end +end |