default_value_for 3.0.3 → 3.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/default_value_for.gemspec +1 -1
- data/lib/default_value_for.rb +12 -6
- data/test.rb +17 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 43f0cd7054b4c0efa902af18ce953a8acf6bb116
|
4
|
+
data.tar.gz: 21bfb3f957a7af4c6ce39b25678bc66c1c8ba1da
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a8535cf9d1197a5ae0a8f10681aee999acd6b95b54e11cb162310226d619f308adf5d4c1b2efc3994810f0f3dd705430d7fad4a43ec65f0f4be4b483aa58fc4
|
7
|
+
data.tar.gz: 7ba0a5fef07ba66e64d6260701ad8c6463be0703dc9677e31e29c05848088f0990ea15b7eccf2cd7c93d2ace569f506e676677257d772744ab25780f226c8982
|
data/default_value_for.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = %q{default_value_for}
|
3
|
-
s.version = "3.0.
|
3
|
+
s.version = "3.0.5"
|
4
4
|
s.summary = %q{Provides a way to specify default values for ActiveRecord models}
|
5
5
|
s.description = %q{The default_value_for plugin allows one to define default values for ActiveRecord models in a declarative manner}
|
6
6
|
s.email = %q{software-signing@phusion.nl}
|
data/lib/default_value_for.rb
CHANGED
@@ -156,12 +156,18 @@ module DefaultValueFor
|
|
156
156
|
|
157
157
|
connection_default_value_defined = new_record? && respond_to?("#{attribute}_changed?") && !__send__("#{attribute}_changed?")
|
158
158
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
159
|
+
attribute_blank = if attributes.has_key?(attribute)
|
160
|
+
column = self.class.columns.detect { |c| c.name == attribute }
|
161
|
+
if column && column.type == :boolean
|
162
|
+
attributes[attribute].nil?
|
163
|
+
else
|
164
|
+
attributes[attribute].blank?
|
165
|
+
end
|
166
|
+
elsif respond_to?(attribute)
|
167
|
+
send(attribute).nil?
|
168
|
+
else
|
169
|
+
instance_variable_get("@#{attribute}").nil?
|
170
|
+
end
|
165
171
|
next unless connection_default_value_defined || attribute_blank
|
166
172
|
|
167
173
|
# allow explicitly setting nil through allow nil option
|
data/test.rb
CHANGED
@@ -49,6 +49,7 @@ ActiveRecord::Base.establish_connection(
|
|
49
49
|
ActiveRecord::Base.connection.create_table(:users, :force => true) do |t|
|
50
50
|
t.string :username
|
51
51
|
t.integer :default_number
|
52
|
+
t.text :settings
|
52
53
|
end
|
53
54
|
|
54
55
|
ActiveRecord::Base.connection.create_table(:books, :force => true) do |t|
|
@@ -233,6 +234,14 @@ class DefaultValuePluginTest < TestCaseClass
|
|
233
234
|
assert_equal 'hi', Book.new.hello
|
234
235
|
end
|
235
236
|
|
237
|
+
def test_works_on_attributes_that_only_have_writers
|
238
|
+
Book.class_eval do
|
239
|
+
default_value_for :hello, "hi"
|
240
|
+
attr_writer :hello
|
241
|
+
end
|
242
|
+
assert_equal 'hi', Book.new.instance_variable_get('@hello')
|
243
|
+
end
|
244
|
+
|
236
245
|
def test_doesnt_conflict_with_overrided_initialize_method_in_model_class
|
237
246
|
Book.class_eval do
|
238
247
|
def initialize(attrs = {})
|
@@ -353,6 +362,14 @@ class DefaultValuePluginTest < TestCaseClass
|
|
353
362
|
assert_equal 1, Book.all.first.number
|
354
363
|
end
|
355
364
|
|
365
|
+
def test_works_with_stored_attribute_accessors_when_initializing_value_that_does_not_allow_nil
|
366
|
+
User.store :settings, :accessors => :bio
|
367
|
+
User.default_value_for :bio, :value => 'None given', :allows_nil => false
|
368
|
+
|
369
|
+
user = User.create!(:bio => 'This is a bio')
|
370
|
+
assert_equal 'This is a bio', user.bio
|
371
|
+
end
|
372
|
+
|
356
373
|
if ActiveRecord::VERSION::MAJOR == 3
|
357
374
|
def test_constructor_ignores_forbidden_mass_assignment_attributes
|
358
375
|
Book.class_eval do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: default_value_for
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.
|
4
|
+
version: 3.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hongli Lai
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-02-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|