simple_model 1.2.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -60,7 +60,7 @@ module SimpleModel
60
60
  (v[:default] && v[:initialize] && (d[k].blank? && (self.class.alias_attributes[k].blank? || d.key?(self.class.alias_attributes[k]) && d[self.class.alias_attributes[k]].blank?)))
61
61
  end
62
62
 
63
- module ClassMethods
63
+ module ClassMethods
64
64
  # Creates a new instance where the attributes store is set to object
65
65
  # provided, which allows one to pass a session store hash or any other
66
66
  # hash-like object to be used for persistance. Typically used for modeling
@@ -80,11 +80,15 @@ module SimpleModel
80
80
  new.set(new.send(:attributes_with_for_init,session_hash))
81
81
  new
82
82
  end
83
-
83
+
84
84
  def alias_attributes
85
85
  @alias_attributes ||= {}.with_indifferent_access
86
86
  end
87
87
 
88
+ def alias_attributes=alias_attributes
89
+ @alias_attributes = alias_attributes
90
+ end
91
+
88
92
  def defined_attributes
89
93
  @defined_attributes ||= {}.with_indifferent_access
90
94
  end
@@ -116,7 +120,7 @@ module SimpleModel
116
120
 
117
121
  def add_defined_attribute(attr,options)
118
122
  self.defined_attributes[attr] = options
119
- define_attribute_methods self.defined_attributes.keys
123
+ define_attribute_methods [attr]
120
124
  end
121
125
 
122
126
  # builds the setter and getter methods
@@ -148,7 +152,10 @@ module SimpleModel
148
152
  val
149
153
  end
150
154
  end
151
-
155
+
156
+ # Creates setter methods for the provided attributes
157
+ # On set, it will mark the attribute as changed if the attributes has been
158
+ # initialized.
152
159
  def define_setter_with_options(attr,options)
153
160
  add_defined_attribute(attr,options)
154
161
  options = default_attribute_settings.merge(options) if (options[:on_set].blank? || options[:after_set].blank?)
@@ -160,7 +167,7 @@ module SimpleModel
160
167
  raise ArgumentError, "#{val} could not be set for #{attr}: #{e.message}"
161
168
  end
162
169
  will_change = "#{attr}_will_change!".to_sym
163
- self.send(will_change) if (self.respond_to?(will_change) && val != self.attributes[attr])
170
+ self.send(will_change) if (initialized?(attr) && val != self.attributes[attr])
164
171
  self.attributes[attr] = val
165
172
  options[:after_set].call(self,val) if options[:after_set]
166
173
  end
@@ -197,11 +204,41 @@ module SimpleModel
197
204
  self.send("#{attribute.to_s}=",*args, &block)
198
205
  end
199
206
  end
207
+
208
+ # Must inherit super's defined_attributes and alias_attributes
209
+ # Rails 3.0 does some weird stuff with ActiveModel::Dirty so we need a
210
+ # hack to keep things working when a class in inherits from a super that
211
+ # has ActiveModel::Dirty included
212
+ def inherited(base)
213
+ # Rails 3.0 Hack
214
+ if (ActiveModel::VERSION::MAJOR == 3 && ActiveModel::VERSION::MINOR == 0)
215
+ base.send(:include, ActiveModel::Dirty)
216
+ base.attribute_method_suffix '_changed?', '_change', '_will_change!', '_was'
217
+ base.attribute_method_affix :prefix => 'reset_', :suffix => '!'
218
+ end
219
+
220
+ base.defined_attributes = self.defined_attributes.merge(base.defined_attributes)
221
+ base.alias_attributes = self.alias_attributes.merge(base.alias_attributes )
222
+ end
200
223
  end
201
224
 
225
+ # Rails 3.0 does some weird stuff with ActiveModel::Dirty so we need a
226
+ # hack to keep things working when a class includes a module that has
227
+ # ActiveModel::Dirty included
202
228
  def self.included(base)
203
- base.extend(Attributes::ClassMethods)
204
- base.send(:include, ActiveModel::Dirty) if base.is_a?(Class) # Add Dirty to the class
229
+ base.extend(Attributes::ClassMethods)
230
+ base.send(:include, ActiveModel::Dirty)
231
+ base.send(:include, ActiveModel::Validations)
232
+ base.send(:include, ActiveModel::Conversion)
233
+ base.extend ActiveModel::Naming
234
+ base.extend ActiveModel::Callbacks
235
+ base.send(:include, ActiveModel::Validations::Callbacks)
236
+
237
+ # Rails 3.0 Hack
238
+ if (ActiveModel::VERSION::MAJOR == 3 && ActiveModel::VERSION::MINOR == 0)
239
+ base.attribute_method_suffix '_changed?', '_change', '_will_change!', '_was'
240
+ base.attribute_method_affix :prefix => 'reset_', :suffix => '!'
241
+ end
205
242
  end
206
243
  end
207
244
  end
@@ -1,20 +1,20 @@
1
- module SimpleModel
1
+ module SimpleModel
2
2
  # Require all that active support we know and love
3
- require 'active_support/time'
4
- require 'active_support/core_ext/class/attribute'
5
- require 'active_support/core_ext/class/attribute_accessors'
6
- require 'active_support/core_ext/class/delegating_attributes'
7
- require 'active_support/core_ext/class/attribute'
8
- require 'active_support/core_ext/array/extract_options'
9
- require 'active_support/core_ext/hash/deep_merge'
10
- require 'active_support/core_ext/hash/indifferent_access'
11
- require 'active_support/core_ext/hash/slice'
12
- require 'active_support/core_ext/string/behavior'
13
- require 'active_support/core_ext/kernel/singleton_class'
14
- require 'active_support/core_ext/module/delegation'
15
- require 'active_support/core_ext/module/introspection'
16
- require 'active_support/core_ext/object/duplicable'
17
- require 'active_support/core_ext/object/blank'
3
+ require 'active_support/time'
4
+ require 'active_support/core_ext/class/attribute'
5
+ require 'active_support/core_ext/class/attribute_accessors'
6
+ require 'active_support/core_ext/class/delegating_attributes'
7
+ require 'active_support/core_ext/class/attribute'
8
+ require 'active_support/core_ext/array/extract_options'
9
+ require 'active_support/core_ext/hash/deep_merge'
10
+ require 'active_support/core_ext/hash/indifferent_access'
11
+ require 'active_support/core_ext/hash/slice'
12
+ require 'active_support/core_ext/string/behavior'
13
+ require 'active_support/core_ext/kernel/singleton_class'
14
+ require 'active_support/core_ext/module/delegation'
15
+ require 'active_support/core_ext/module/introspection'
16
+ require 'active_support/core_ext/object/duplicable'
17
+ require 'active_support/core_ext/object/blank'
18
18
 
19
19
  # == SimpleModel::Base
20
20
  #
@@ -76,12 +76,8 @@ module SimpleModel
76
76
  include SimpleModel::Attributes
77
77
  include SimpleModel::ErrorHelpers
78
78
  #Use ActiveModel Resources
79
- include ActiveModel::Validations
80
- include ActiveModel::Conversion
81
- extend ActiveModel::Naming
82
- extend ActiveModel::Callbacks
83
- include ActiveModel::Validations::Callbacks
84
79
 
80
+
85
81
  define_model_callbacks :save, :update, :create, :destroy
86
82
 
87
83
  class << self
@@ -122,7 +118,7 @@ module SimpleModel
122
118
  if completed
123
119
  self.persisted = true
124
120
  @previously_changed = changes
125
- @changed_attributes.clear
121
+ @changed_attributes.clear
126
122
  else
127
123
  self.send(options[:rollback]) unless options[:rollback].blank?
128
124
  end
@@ -1,5 +1,5 @@
1
1
  module SimpleModel
2
- class SimpleModelRailtie < ::Rails::Railtie
2
+ class SimpleModelRailtie < ::Rails::Railtie
3
3
  initializer "simple_model.extend_core" do
4
4
  extend SimpleModel::ExtendCore
5
5
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleModel
2
- VERSION = "1.2.2"
2
+ VERSION = "1.2.3"
3
3
  end
@@ -60,6 +60,7 @@ describe SimpleModel::Attributes do
60
60
  "hop" if nap
61
61
  end
62
62
  end
63
+
63
64
  end
64
65
 
65
66
  before(:each) do
@@ -154,30 +155,44 @@ describe SimpleModel::Attributes do
154
155
 
155
156
  lambda{TestThrow.new(:boo => [])}.should raise_error(SimpleModel::ArgumentError)
156
157
  end
157
- context '#alias_attribute' do
158
- it "should create alias for attribute" do
159
- class TestAlias
158
+ context '#alias_attribute' do
159
+ it "should create alias for attribute" do
160
+ class TestAlias
161
+ include SimpleModel::Attributes
162
+ has_attribute :foo, :default => "bar"
163
+ alias_attribute(:bar,:foo)
164
+ end
165
+
166
+ t = TestAlias.new(:bar => "foo")
167
+ t.bar.should eql("foo")
168
+ t.foo.should eql('foo')
169
+ t = TestAlias.new(:foo => "foo")
170
+ t.bar.should eql("foo")
171
+ t.foo.should eql('foo')
172
+ end
173
+ end
174
+ end
175
+
176
+ context "regression tests" do
177
+ it "should merge defined attributes when class are inhereted" do
178
+ class MyBase
160
179
  include SimpleModel::Attributes
161
- has_attribute :foo, :default => "bar"
162
- alias_attribute(:bar,:foo)
180
+ has_boolean :bar
163
181
  end
164
-
165
- t = TestAlias.new(:bar => "foo")
166
- t.bar.should eql("foo")
167
- t.foo.should eql('foo')
168
- t = TestAlias.new(:foo => "foo")
169
- t.bar.should eql("foo")
170
- t.foo.should eql('foo')
182
+
183
+ class NewerBase < MyBase
184
+ has_boolean :foo
185
+ end
186
+
187
+ NewerBase.defined_attributes[:bar].blank?.should be_false
188
+ n = NewerBase.new
189
+ n.respond_to?(:bar_will_change!).should be_true
171
190
  end
172
191
  end
173
-
174
- end
175
-
176
192
 
177
193
  after(:all) do
178
- Object.send(:remove_const,:TestThrow)
179
- Object.send(:remove_const,:OnGet)
180
- Object.send(:remove_const,:TestDefault)
181
- Object.send(:remove_const,:TestInit)
194
+ [:TestThrow,:OnGet,:TestDefault,:TestInit,:MyBase,:NewerBase].each do |test_klass|
195
+ Object.send(:remove_const,test_klass)
196
+ end
182
197
  end
183
198
  end
@@ -2,9 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe SimpleModel do
4
4
 
5
- after(:each) do
6
- Object.send(:remove_const,:TestStuff)
7
- end
5
+
8
6
 
9
7
  context 'action methods' do
10
8
  describe "save" do
@@ -188,6 +186,7 @@ describe SimpleModel do
188
186
  class TestStuff < SimpleModel::Base
189
187
  save :my_save_method
190
188
  has_attributes :foo,:bar, :default => "def"
189
+ has_boolean :boo,:bad, :default => true
191
190
  def my_save_method
192
191
  true
193
192
  end
@@ -196,9 +195,39 @@ describe SimpleModel do
196
195
  t = TestStuff.new
197
196
  t.foo = "bar"
198
197
  t.foo_changed?.should be_true
198
+ t.respond_to?(:foo_will_change!).should be_true
199
+ t.respond_to?(:boo_will_change!).should be_true
199
200
  t.foo_change.should eql(["def","bar"])
200
201
  t.changed?.should be_true
201
202
  t.save
202
203
  t.changed?.should be_false
203
- end
204
+ end
205
+
206
+ context "regression tests" do
207
+ before(:each) do
208
+ class TestStuff < SimpleModel::Base
209
+ has_attribute :bar
210
+ end
211
+
212
+ class NewTestStuff < TestStuff
213
+ has_boolean :foo
214
+ end
215
+ end
216
+ it "should merge defined attributes when class are inhereted" do
217
+ NewTestStuff.defined_attributes[:bar].blank?.should be_false
218
+ NewTestStuff.defined_attributes[:foo].blank?.should be_false
219
+ end
220
+ it "should merge defined attributes when class are inhereted" do
221
+ TestStuff.new.respond_to?(:bar_will_change!).should be_true
222
+ NewTestStuff.new.respond_to?(:bar_will_change!).should be_true
223
+ NewTestStuff.new.respond_to?(:foo_will_change!).should be_true
224
+ end
225
+ after(:each) do
226
+ Object.send(:remove_const,:NewTestStuff)
227
+ end
228
+ end
229
+
230
+ after(:each) do
231
+ Object.send(:remove_const,:TestStuff)
232
+ end
204
233
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-18 00:00:00.000000000 Z
12
+ date: 2012-10-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport