Skip to content
Browse files

Add ActiveModel::Model, a mixin to make Ruby objects to work with AP …

…inmediatly
  • Loading branch information...
1 parent 14f06dd commit 3b822e91d1a6c4eab0064989bbd07aae3a6d0d08 @guilleiguaran guilleiguaran committed Mar 2, 2012
View
2 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*
View
1 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'
View
22 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
View
19 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
Ruby on Rails member

👍

@josevalim
Ruby on Rails member

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
Ruby on Rails member

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

@carlosantoniodasilva
Ruby on Rails member

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

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

@josevalim
Ruby on Rails member

The current docs look great. Thanks! <3

@cgriego
cgriego commented on 3b822e9 Mar 8, 2012

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
Ruby on Rails member

@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
Ruby on Rails member
wycats commented on 3b822e9 Mar 8, 2012

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.
Something went wrong with that request. Please try again.