mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #5253 from guilleiguaran/amo-model
Add ActiveModel::Model, a mixin to make plain Ruby objects work with AP out of box
This commit is contained in:
commit
eafa58b566
4 changed files with 44 additions and 0 deletions
|
@ -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'
|
||||
|
|
22
activemodel/lib/active_model/model.rb
Normal file
22
activemodel/lib/active_model/model.rb
Normal file
|
@ -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
activemodel/test/cases/model_test.rb
Normal file
19
activemodel/test/cases/model_test.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue