Skip to content

Commit

Permalink
Add ActiveModel::Model, a mixin to make Ruby objects to work with AP …
Browse files Browse the repository at this point in the history
…inmediatly
  • Loading branch information
guilleiguaran committed Mar 3, 2012
1 parent 14f06dd commit 3b822e9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
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

8 comments on commit 3b822e9

@josevalim
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@josevalim
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, this is missing docs. Can we please push some docs to ActiveModel::Model, explain how to use it and mention how other Active Model modules could be included?

@jonleighton
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nice! Great job. Can we use public_send rather than send to set the attributes though?

@carlosantoniodasilva
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For further reference, docs were added and send is now public_send.

@josevalim do you think we need more docs on that?

@josevalim
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current docs look great. Thanks! <3

@cgriego
Copy link
Contributor

@cgriego cgriego commented on 3b822e9 Mar 8, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is an excellent direction for ActiveModel to be moving. But I'm afraid that including the mass assignment on initialization muddles the architecture of ActiveModel. What people are asking for is the simplest possible way to conform to the ActiveModel API, but by adding the initialize method you've gone a step further. I agree with wanting to provide something ActiveRecord-like, but this initialize method isn't ActiveRecord like. It doesn't incorporate mass assignment security (a hot topic lately), or the secondary option hash argument. It also isn't in line with ActiveModel's pick-and-choose library of modules approach.

What if initialize was removed and ActiveModel::Model was renamed to ActiveModel::BasicModel, the bare minimum to comply with the ActiveModel API, and then ActiveModel::Model can combine it and other modules, implementing no functionality itself, to give people the curated functionality people have come to expect from Rails.

@jonleighton
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cgriego I am not sure I see the issue given that a user can very easily overload initialize and implement their own functionality. Seems like extra ceremony to split behaviour into another module.

Renaming it to ActiveModel::Basic would be nice though, IMO.

@wycats
Copy link
Member

@wycats wycats commented on 3b822e9 Mar 8, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way I usually build AMo models is with an attributes hash and read_attributes delegating to it. That way I don't have to define accessors if I don't want to.

Please sign in to comment.