nifty-utils 1.0.7 → 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 949a5348ccca0521b44ae3113799185f7be7c9e9
4
- data.tar.gz: b9535c88e4899f4602c2098b82768d362529cb98
3
+ metadata.gz: ed647a6e5da31152d8a3295a73a73c2b570d73ae
4
+ data.tar.gz: 577aecf0562f1ce708f280a6d67c4d3d5f3bf3ca
5
5
  SHA512:
6
- metadata.gz: c1badcc5f2c3aba56d2bd727b12ee7b391c32c186e81853acdb186a9df0caef27aae9237393aee52a79ff3755e1b41bcc4a40b169ea6cefc7ce5648fb1784bf1
7
- data.tar.gz: 6096f7a2d95eb588b5c48368051c9235d9c41e7f1b23e06914a3248415eace3e7cfd2fdaf39057bbd4c18210a255ba3fee1a8798af3e848c803397b0a20643d1
6
+ metadata.gz: dbb9adbabaeebe0e34909566afc7db2393d4b4a77e2ba4e62d5dad4c4bba354bc2821b001bf763d386540c92dafa1897a88383f3dcfeab7b1bfa7940e4cf9d6e
7
+ data.tar.gz: cd1daea8f5b5233188a5aee2817bd2567b9ad51fcaaf563d5ff673511abedc1aa3011e853cc6f5bec0d89130d654ca008b94e8ab06f90b92ef3e0e5d9d256f22
@@ -0,0 +1,56 @@
1
+ module Nifty
2
+ module Utils
3
+ module ActiveRecord
4
+ module DefaultValue
5
+
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+
12
+ def has_default_values?
13
+ false
14
+ end
15
+
16
+ def default_value(field, block, options = {})
17
+ self.send :include, ModelExtensions unless self.has_default_values?
18
+ self.default_value_definitions[field] = options.merge(:block => block)
19
+ end
20
+
21
+ end
22
+
23
+ module ModelExtensions
24
+
25
+ def self.included(base)
26
+ base.extend ClassMethods
27
+ base.before_validation :_set_default_values
28
+ end
29
+
30
+ def _set_default_values
31
+ self.class.default_value_definitions.each do |field, opts|
32
+ if self.send(field).blank?
33
+ proposed_value = self.instance_exec(&opts[:block])
34
+ self.send("#{field}=", proposed_value)
35
+ end
36
+ end
37
+ end
38
+
39
+ module ClassMethods
40
+
41
+ def default_value_definitions
42
+ @default_value_definitions ||= {}
43
+ end
44
+
45
+ def has_default_values?
46
+ true
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+ end
55
+ end
56
+ end
@@ -10,6 +10,8 @@ module Nifty
10
10
  ::ActiveRecord::Base.send :include, Nifty::Utils::ActiveRecord::Inquirer
11
11
  require 'nifty/utils/active_record/random_string'
12
12
  ::ActiveRecord::Base.send :include, Nifty::Utils::ActiveRecord::RandomString
13
+ require 'nifty/utils/active_record/default_value'
14
+ ::ActiveRecord::Base.send :include, Nifty::Utils::ActiveRecord::DefaultValue
13
15
  end
14
16
 
15
17
  # load the Action View helpers
@@ -1,5 +1,5 @@
1
1
  module Nifty
2
2
  module Utils
3
- VERSION = '1.0.7'
3
+ VERSION = '1.0.8'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nifty-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.7
4
+ version: 1.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Cooke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-14 00:00:00.000000000 Z
11
+ date: 2015-01-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A set of useful utilties for Rails applications
14
14
  email:
@@ -18,6 +18,7 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - lib/nifty/utils.rb
21
+ - lib/nifty/utils/active_record/default_value.rb
21
22
  - lib/nifty/utils/active_record/inquirer.rb
22
23
  - lib/nifty/utils/active_record/random_string.rb
23
24
  - lib/nifty/utils/extensions/core.rb
@@ -50,4 +51,3 @@ signing_key:
50
51
  specification_version: 4
51
52
  summary: A set of useful utilties for Rails applications
52
53
  test_files: []
53
- has_rdoc: