Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ActiveModel::Model, a mixin to make plain Ruby objects work with AP out of box #5253

Merged
merged 1 commit into from Mar 3, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions 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*
Expand Down
1 change: 1 addition & 0 deletions activemodel/lib/active_model.rb
Expand Up @@ -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'
Expand Down
22 changes: 22 additions & 0 deletions 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
19 changes: 19 additions & 0 deletions 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