defaulter 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9a7192e85a9ca8007a51b96f77e3b5bceac41116
4
+ data.tar.gz: 0816ab4273b2017384e3d424e925ce19b0594947
5
+ SHA512:
6
+ metadata.gz: 5dfe4477e12dcde4a941c8df9e3ea52d9da5a11be1e2555ea5593d54458337fcf680e0a436ad9b6c0ce5c26d6ce5690e7346b642053c5c5cbc4935151786f1bc
7
+ data.tar.gz: eadd54daeeeadf613c69a01572b35acdc27035b9d6ce601fbb29484d3fefbe136e41499f0581ac466deb99041274aa5fdb2a2d3ae6c007f902110e6ef4ac1792
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Defaulter
1
+ # Defaulter [![Gem Version](https://badge.fury.io/rb/defaulter.png)](http://badge.fury.io/rb/defaulter)
2
2
 
3
3
  Defaulter allows you to mark and maintain default objects in ActiveRecord association collections with minimal code and minimal fuss.
4
4
 
@@ -1,58 +1,90 @@
1
1
  module Defaulter
2
2
  module HasDefault
3
3
  def has_default(model, *args)
4
- options = args.extract_options!
4
+ resource = model.to_sym
5
+ options = args.extract_options!
6
+ resources = resource.to_s.pluralize.to_sym
7
+ column_name = options.delete(:column) || :prime
8
+ column_setter = "#{column_name}=".to_sym
9
+
10
+ # Unused
11
+ # method = "default_#{resource}_column".to_sym
12
+ # class_variable_set("@@#{method}".to_sym, column_name.freeze)
13
+ # define_singleton_method(method) { class_variable_get("@@#{method}".to_sym) }
5
14
 
6
15
  def load_method
7
- @@load_method ||= (Gem::Version.new('4.0.0beta') >= Gem::Version.new(Rails.version)) ? :all : :load
16
+ @@load_method ||= (Gem::Version.new('4.0.0') >=
17
+ Gem::Version.new(Rails.version)) ? :all : :load
8
18
  end
9
19
 
10
- has_many model.to_s.pluralize.to_sym, options do
20
+ has_many resources, options do
11
21
 
12
- def default
13
- where(prime: true).limit(1).first
14
- end
22
+ define_method(:default) { where(column_name => true).limit(1)[0] }
15
23
 
16
- def default=(record)
17
- records = self.send(load_method)
24
+ define_method(:default=) do |record|
25
+ records = self.send(load_method)
18
26
 
19
27
  if records.include?(record)
20
28
  ActiveRecord::Base.transaction do
21
29
  # Marking existing record as default
22
- records.map { |r| r.update_attribute(:prime, false) if r.prime? }
23
- record.update_attribute(:prime, true)
30
+ records.map { |r| r.update_attribute(column_name, false) if
31
+ r.send(column_name) }
32
+ record.update_attribute(column_name, true)
24
33
  end
25
34
  else
26
35
  raise ActiveRecord::RecordNotFound, "Record not in collection"
27
36
  end
28
37
  end
29
38
 
30
- def << (models)
31
- primes = nil
39
+ define_method(:<<) do |records|
40
+ records_on_db = self.send(load_method)
41
+ none_on_db = records_on_db.empty?
42
+ default_on_db = records_on_db.select { |r| r.send(column_name) }
32
43
 
33
- case models.class.name
44
+ case records.class.name
34
45
  when 'Array'
35
- models.first.prime = true if self.send(load_method).empty? && primes.empty? && !models.blank?
36
- primes = models.select { |m| m.prime? }
37
- model = models.delete(primes.last)
46
+ # Find out if any default records have been given
47
+ default_records = records.select do |r|
48
+ r.send(column_name)
49
+ end
50
+
51
+ # Mark the first record as default iff:
52
+ # 1. None exist on DB
53
+ # 2. Given array of records is NOT empty
54
+ # 3. Given records have no default record marked
55
+ records[0].send(column_setter, true) if
56
+ none_on_db &&
57
+ !records.empty? &&
58
+ default_records.empty?
59
+
60
+ # Candidate default record if none given or none on db
61
+ default_record = records.delete(default_records[-1])
38
62
 
39
63
  ActiveRecord::Base.transaction do
40
- if model.blank?
41
- self.concat(models) unless models.blank?
64
+ if default_record.blank?
65
+ self.concat(records) unless records.blank?
42
66
  else
43
- models.each { |m| m.prime = false }
44
- self.where(prime: true).each { |r| r.update_attribute(:prime, false) }
45
- self.concat(models + [model])
67
+ records.each { |r| r.send(column_setter, false) }
68
+
69
+ default_on_db.each do |r|
70
+ r.update_attribute(column_name, false)
71
+ end
72
+
73
+ self.concat(records + [default_record])
46
74
  end
47
75
  end
48
76
  else
49
- unless models.blank?
50
- loaded = !self.send(load_method).empty?
51
- models.prime = true unless loaded
77
+ unless records.blank?
78
+ records.send(column_setter, true) if default_on_db.blank?
52
79
 
53
80
  ActiveRecord::Base.transaction do
54
- self.where(prime: true).each { |r| r.update_attribute(:prime, false) } if loaded && models.prime?
55
- self.concat(models)
81
+ if records.send(column_name)
82
+ default_on_db.map do |r|
83
+ r.update_attribute(column_name, false)
84
+ end
85
+ end
86
+
87
+ self.concat(records)
56
88
  end
57
89
  end
58
90
  end
@@ -1,3 +1,3 @@
1
1
  module Defaulter
2
- VERSION = "0.0.9"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,46 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: defaulter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
5
- prerelease:
4
+ version: 0.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Amol Hatwar
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-01-29 00:00:00.000000000 Z
11
+ date: 2014-03-12 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rails
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: 3.2.0
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: 3.2.0
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: sqlite3
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  description: Mark and maintain default ActiveRecord records.
@@ -50,19 +45,22 @@ executables: []
50
45
  extensions: []
51
46
  extra_rdoc_files: []
52
47
  files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - lib/defaulter.rb
53
52
  - lib/defaulter/has_default.rb
54
53
  - lib/defaulter/version.rb
55
- - lib/defaulter.rb
56
54
  - lib/tasks/defaulter_tasks.rake
57
- - MIT-LICENSE
58
- - Rakefile
59
- - README.md
60
55
  - test/defaulter_test.rb
56
+ - test/dummy/README.rdoc
57
+ - test/dummy/Rakefile
61
58
  - test/dummy/app/assets/javascripts/application.js
62
59
  - test/dummy/app/assets/stylesheets/application.css
63
60
  - test/dummy/app/controllers/application_controller.rb
64
61
  - test/dummy/app/helpers/application_helper.rb
65
62
  - test/dummy/app/views/layouts/application.html.erb
63
+ - test/dummy/config.ru
66
64
  - test/dummy/config/application.rb
67
65
  - test/dummy/config/boot.rb
68
66
  - test/dummy/config/database.yml
@@ -78,40 +76,35 @@ files:
78
76
  - test/dummy/config/initializers/wrap_parameters.rb
79
77
  - test/dummy/config/locales/en.yml
80
78
  - test/dummy/config/routes.rb
81
- - test/dummy/config.ru
82
79
  - test/dummy/db/test.sqlite3
83
- - test/dummy/log/test.log
84
80
  - test/dummy/public/404.html
85
81
  - test/dummy/public/422.html
86
82
  - test/dummy/public/500.html
87
83
  - test/dummy/public/favicon.ico
88
- - test/dummy/Rakefile
89
- - test/dummy/README.rdoc
90
84
  - test/dummy/script/rails
91
85
  - test/test_helper.rb
92
86
  homepage: https://github.com/AxisOfEval/defaulter
93
87
  licenses: []
88
+ metadata: {}
94
89
  post_install_message:
95
90
  rdoc_options: []
96
91
  require_paths:
97
92
  - lib
98
93
  required_ruby_version: !ruby/object:Gem::Requirement
99
- none: false
100
94
  requirements:
101
- - - ! '>='
95
+ - - ">="
102
96
  - !ruby/object:Gem::Version
103
97
  version: '0'
104
98
  required_rubygems_version: !ruby/object:Gem::Requirement
105
- none: false
106
99
  requirements:
107
- - - ! '>='
100
+ - - ">="
108
101
  - !ruby/object:Gem::Version
109
102
  version: '0'
110
103
  requirements: []
111
104
  rubyforge_project:
112
- rubygems_version: 1.8.24
105
+ rubygems_version: 2.2.0
113
106
  signing_key:
114
- specification_version: 3
107
+ specification_version: 4
115
108
  summary: Often ActiveRecord is used to return a collection of objects, for example
116
109
  a user can have many email addresses. But which email address to use to send an
117
110
  email? That is where marking one object in the collection as default comes in. The
@@ -140,7 +133,6 @@ test_files:
140
133
  - test/dummy/config/routes.rb
141
134
  - test/dummy/config.ru
142
135
  - test/dummy/db/test.sqlite3
143
- - test/dummy/log/test.log
144
136
  - test/dummy/public/404.html
145
137
  - test/dummy/public/422.html
146
138
  - test/dummy/public/500.html
@@ -1,9 +0,0 @@
1
- Connecting to database specified by database.yml
2
-  (4.6ms) begin transaction
3
-  (0.0ms) rollback transaction
4
- Connecting to database specified by database.yml
5
-  (3.5ms) begin transaction
6
-  (0.1ms) rollback transaction
7
- Connecting to database specified by database.yml
8
-  (0.3ms) begin transaction
9
-  (0.0ms) rollback transaction