Skip to content

Commit 3b822e9

Browse files
committed
Add ActiveModel::Model, a mixin to make Ruby objects to work with AP inmediatly
1 parent 14f06dd commit 3b822e9

4 files changed

Lines changed: 44 additions & 0 deletions

File tree

activemodel/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Added ActiveModel::Model, a mixin to make Ruby objects work with AP out of box *Guillermo Iguaran*
2+
13
* `AM::Errors#to_json`: support `:full_messages` parameter *Bogdan Gusiev*
24

35
* Trim down Active Model API by removing `valid?` and `errors.full_messages` *José Valim*

activemodel/lib/active_model.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module ActiveModel
3939
autoload :Errors
4040
autoload :Lint
4141
autoload :MassAssignmentSecurity
42+
autoload :Model
4243
autoload :Name, 'active_model/naming'
4344
autoload :Naming
4445
autoload :Observer, 'active_model/observing'
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)