simple_model 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec CHANGED
@@ -1 +1,2 @@
1
- --color
1
+ --color
2
+ --debug
@@ -45,14 +45,21 @@ module SimpleModel
45
45
  end
46
46
 
47
47
  # Returns attribute that have defaults in a hash: {:attrbute => "default value"}
48
+ # Checks for alias attributes to ensure they are not overwritten
48
49
  def attributes_with_for_init(attrs)
49
50
  d = attrs.with_indifferent_access
50
51
  self.class.defined_attributes.each do |k,v|
51
- d[k] = fetch_default_value(v[:default]) if (d[k].blank? && v[:default] && v[:initialize])
52
+ if allow_set_default?(d,k,v)
53
+ d[k] = fetch_default_value(v[:default])
54
+ end
52
55
  end
53
56
  d
54
57
  end
55
58
 
59
+ def allow_set_default?(d,k,v)
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
+ end
62
+
56
63
  module ClassMethods
57
64
  # Creates a new instance where the attributes store is set to object
58
65
  # provided, which allows one to pass a session store hash or any other
@@ -74,9 +81,12 @@ module SimpleModel
74
81
  new
75
82
  end
76
83
 
84
+ def alias_attributes
85
+ @alias_attributes ||= {}.with_indifferent_access
86
+ end
77
87
 
78
88
  def defined_attributes
79
- @defined_attributes ||= {}
89
+ @defined_attributes ||= {}.with_indifferent_access
80
90
  end
81
91
 
82
92
  def defined_attributes=defined_attributes
@@ -179,6 +189,7 @@ module SimpleModel
179
189
  # Creates alias setter and getter for the supplied attribute using the supplied alias
180
190
  # See spec for example.
181
191
  def alias_attribute(new_alias,attribute)
192
+ alias_attributes[attribute] = new_alias
182
193
  define_method(new_alias) do
183
194
  self.send(attribute)
184
195
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleModel
2
- VERSION = "1.2.1"
2
+ VERSION = "1.2.2"
3
3
  end
@@ -158,7 +158,7 @@ describe SimpleModel::Attributes do
158
158
  it "should create alias for attribute" do
159
159
  class TestAlias
160
160
  include SimpleModel::Attributes
161
- has_attribute :foo
161
+ has_attribute :foo, :default => "bar"
162
162
  alias_attribute(:bar,:foo)
163
163
  end
164
164
 
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.1
4
+ version: 1.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: