Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Owner

:+1:

@josevalim
Owner

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
Collaborator

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

@carlosantoniodasilva

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

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

@josevalim
Owner

The current docs look great. Thanks! <3

@cgriego

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
Collaborator

@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
Collaborator

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.