Permalink
Browse files

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

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

This comment has been minimized.

Show comment
Hide comment
@josevalim

josevalim Mar 3, 2012

Contributor

👍

Contributor

josevalim replied Mar 3, 2012

👍

@josevalim

This comment has been minimized.

Show comment
Hide comment
@josevalim

josevalim Mar 3, 2012

Contributor

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?

Contributor

josevalim replied Mar 3, 2012

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

This comment has been minimized.

Show comment
Hide comment
@jonleighton

jonleighton Mar 3, 2012

Member

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

Member

jonleighton replied Mar 3, 2012

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

@carlosantoniodasilva

This comment has been minimized.

Show comment
Hide comment
@carlosantoniodasilva

carlosantoniodasilva Mar 5, 2012

Member

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

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

Member

carlosantoniodasilva replied Mar 5, 2012

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

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

@josevalim

This comment has been minimized.

Show comment
Hide comment
@josevalim

josevalim Mar 5, 2012

Contributor

The current docs look great. Thanks! <3

Contributor

josevalim replied Mar 5, 2012

The current docs look great. Thanks! <3

@cgriego

This comment has been minimized.

Show comment
Hide comment
@cgriego

cgriego Mar 8, 2012

Contributor

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.

Contributor

cgriego replied 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

This comment has been minimized.

Show comment
Hide comment
@jonleighton

jonleighton Mar 8, 2012

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.

Member

jonleighton replied Mar 8, 2012

@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

This comment has been minimized.

Show comment
Hide comment
@wycats

wycats Mar 8, 2012

Member

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.

Member

wycats replied 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.