Skip to content
This repository
Browse code

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

…inmediatly
  • Loading branch information...
commit 3b822e91d1a6c4eab0064989bbd07aae3a6d0d08 1 parent 14f06dd
Guillermo Iguaran authored March 02, 2012
2  activemodel/CHANGELOG.md
Source Rendered
... ...
@@ -1,3 +1,5 @@
  1
+*   Added ActiveModel::Model, a mixin to make Ruby objects work with AP out of box *Guillermo Iguaran*
  2
+
1 3
 *   `AM::Errors#to_json`: support `:full_messages` parameter *Bogdan Gusiev*
2 4
 
3 5
 *   Trim down Active Model API by removing `valid?` and `errors.full_messages` *José Valim*
1  activemodel/lib/active_model.rb
@@ -39,6 +39,7 @@ module ActiveModel
39 39
   autoload :Errors
40 40
   autoload :Lint
41 41
   autoload :MassAssignmentSecurity
  42
+  autoload :Model
42 43
   autoload :Name, 'active_model/naming'
43 44
   autoload :Naming
44 45
   autoload :Observer, 'active_model/observing'
22  activemodel/lib/active_model/model.rb
... ...
@@ -0,0 +1,22 @@
  1
+module ActiveModel
  2
+  module Model
  3
+    def self.included(base)
  4
+      base.class_eval do
  5
+        extend  ActiveModel::Naming
  6
+        extend  ActiveModel::Translation
  7
+        include ActiveModel::Validations
  8
+        include ActiveModel::Conversion
  9
+      end
  10
+    end
  11
+
  12
+    def initialize(params={})
  13
+      params.each do |attr, value|
  14
+        self.send(:"#{attr}=", value)
  15
+      end if params
  16
+    end
  17
+
  18
+    def persisted?
  19
+      false
  20
+    end
  21
+  end
  22
+end
19  activemodel/test/cases/model_test.rb
... ...
@@ -0,0 +1,19 @@
  1
+require 'cases/helper'
  2
+
  3
+class ModelTest < ActiveModel::TestCase
  4
+  include ActiveModel::Lint::Tests
  5
+
  6
+  class BasicModel
  7
+    include ActiveModel::Model
  8
+    attr_accessor :attr
  9
+  end
  10
+
  11
+  def setup
  12
+    @model = BasicModel.new
  13
+  end
  14
+
  15
+  def test_initialize_with_params
  16
+    object = BasicModel.new(:attr => "value")
  17
+    assert_equal object.attr, "value"
  18
+  end
  19
+end

8 notes on commit 3b822e9

José Valim
Owner

:+1:

José Valim
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?

Jon Leighton
Owner

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

Carlos Antonio da Silva

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

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

José Valim
Owner

The current docs look great. Thanks! <3

Chris Griego

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.

Jon Leighton
Owner

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

Yehuda Katz
Owner

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.