primary 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +8 -2
  3. data/lib/primary.rb +40 -36
  4. data/lib/primary/version.rb +1 -1
  5. metadata +53 -39
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8efa6a7557ad39b2c676ec441ad06395ebf70690eb2a787ad7595df889c5040a
4
+ data.tar.gz: 5fde59f8a67acda63d2829902f4e257d42e20d9888aea4b9b85d7f9631bc382f
5
+ SHA512:
6
+ metadata.gz: 813c2dbeed6c57d7d0751968a88ce5756925ea92dcbe3d044d0c4f0bc3d310849e52694a4a67bdc617c91e40515a8f40f44c50a4862175a54dd9800fe76446e5
7
+ data.tar.gz: 77ad9f6c840a545cf7c355a5b73ac02e764aa7d998049fa64dc6edba0ead18447f612529314c71b918696e81d551085c1f794d3c10e4082b13b8e5843cf0b2bd
data/README.md CHANGED
@@ -29,7 +29,7 @@ lang_en.is_primary
29
29
  # => true
30
30
 
31
31
  lang_de = Language.create({:language=>'de'})
32
- lang_de.is.primary
32
+ lang_de.is_primary
33
33
  # => false
34
34
 
35
35
  lang_de.is_primary = true
@@ -49,6 +49,12 @@ is_primary :on=>'is_default', :scope=>'site_id'
49
49
  :scope => 'site_id' # scope default to different column - ie. you'll
50
50
  # be able to define multiple languages per multiple
51
51
  # sites and each site will have one default lang.
52
+
53
+ # Scope can be defined as array of params
54
+ :scope => ['site_id', 'language_code']
55
+ :scope => ['polymorphic_id', 'polymorphic_type']
56
+ :auto_primary_record => false # if you don't want primary record
57
+ # to be set automatically (default: true)
52
58
  ```
53
59
 
54
60
  ##Requirements
@@ -57,4 +63,4 @@ Currently gem requires Rails 3.2, I'll have to check, if it works as desired wit
57
63
 
58
64
  ##License
59
65
 
60
- Project is distributed under general terms of MIT-LICENSE.
66
+ Project is distributed under general terms of MIT-LICENSE.
data/lib/primary.rb CHANGED
@@ -1,54 +1,58 @@
1
1
  module Primary
2
- def is_primary options={}
2
+ def is_primary(options = {})
3
3
  cattr_accessor :primary_is_primary_opts
4
- self.primary_is_primary_opts = options.reverse_merge({
5
- :on => :is_primary,
6
- :scope => nil
7
- })
4
+ self.primary_is_primary_opts = options.reverse_merge(
5
+ {
6
+ on: :is_primary,
7
+ scope: nil,
8
+ auto_primary_record: true
9
+ }
10
+ )
8
11
 
9
12
  before_save :primary_is_primary_mark
10
13
  after_save :primary_is_primary_take_care
11
14
  include InstanceMethods
12
15
  end
16
+
13
17
  module InstanceMethods
14
18
  private
15
- def primary_is_primary_mark
16
- options = self.class.primary_is_primary_opts
17
- check = get_primary_scope(options)
18
- if check.count == 0
19
- self.send("#{options[:on]}=", true)
20
- end
21
- end
22
- def primary_is_primary_take_care
23
- options = self.class.primary_is_primary_opts
24
- if self.send("#{options[:on]}?")==true
25
- scope = get_primary_scope(options)
26
- scope = scope.where('id!=?', self.id)
27
- scope.update_all(["#{options[:on]}=?", false])
28
- end
19
+
20
+ def primary_is_primary_mark
21
+ options = self.class.primary_is_primary_opts
22
+ return unless !!options[:auto_primary_record]
23
+
24
+ check = get_primary_scope(options)
25
+ send("#{options[:on]}=", true) if check.count == 0
26
+ end
27
+
28
+ def primary_is_primary_take_care
29
+ options = self.class.primary_is_primary_opts
30
+ if send("#{options[:on]}?") == true
31
+ scope = get_primary_scope(options)
32
+ scope = scope.where('id!=?', id)
33
+ scope.update_all(["#{options[:on]}=?", false])
29
34
  end
35
+ end
30
36
 
31
- def get_primary_scope(options)
32
- check = self.class.where("#{options[:on].to_s} = ?", true)
33
- if options[:scope]
34
- sc = options[:scope]
35
- if sc.is_a?(Symbol) or sc.is_a?(String)
36
- check = build_primary_scope(check, sc)
37
- elsif sc.is_a?(Array)
38
- sc.each do |scope_elem|
39
- check = build_primary_scope(check, scope_elem)
40
- end
37
+ def get_primary_scope(options)
38
+ check = self.class.default_scoped.where("#{options[:on]} = ?", true)
39
+ if options[:scope]
40
+ sc = options[:scope]
41
+ if sc.is_a?(Symbol) || sc.is_a?(String)
42
+ check = build_primary_scope(check, sc)
43
+ elsif sc.is_a?(Array)
44
+ sc.each do |scope_elem|
45
+ check = build_primary_scope(check, scope_elem)
41
46
  end
42
47
  end
43
- check
44
48
  end
49
+ check
50
+ end
45
51
 
46
- def build_primary_scope(scope_chain, elem)
47
- if elem.is_a?(String)
48
- elem = elem.to_sym
49
- end
50
- scope_chain.where("#{elem} = ?", self.send(elem))
51
- end
52
+ def build_primary_scope(scope_chain, elem)
53
+ elem = elem.to_sym if elem.is_a?(String)
54
+ scope_chain.where("#{elem} = ?", send(elem))
55
+ end
52
56
  end
53
57
  end
54
58
  ActiveRecord::Base.extend Primary
@@ -1,3 +1,3 @@
1
1
  module Primary
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,82 +1,99 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: primary
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
- prerelease:
4
+ version: 0.0.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - Maciej Litwiniuk
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-07-08 00:00:00.000000000Z
11
+ date: 2021-08-18 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rails
16
- requirement: &70344345470500 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: 3.0.0
22
20
  type: :runtime
23
21
  prerelease: false
24
- version_requirements: *70344345470500
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
25
27
  - !ruby/object:Gem::Dependency
26
28
  name: sqlite3
27
- requirement: &70344345470000 !ruby/object:Gem::Requirement
28
- none: false
29
+ requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
- - - ! '>='
31
+ - - ">="
31
32
  - !ruby/object:Gem::Version
32
33
  version: '0'
33
34
  type: :development
34
35
  prerelease: false
35
- version_requirements: *70344345470000
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
36
41
  - !ruby/object:Gem::Dependency
37
42
  name: rspec-rails
38
- requirement: &70344345469380 !ruby/object:Gem::Requirement
39
- none: false
43
+ requirement: !ruby/object:Gem::Requirement
40
44
  requirements:
41
- - - ! '>='
45
+ - - ">="
42
46
  - !ruby/object:Gem::Version
43
47
  version: '0'
44
48
  type: :development
45
49
  prerelease: false
46
- version_requirements: *70344345469380
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
47
55
  - !ruby/object:Gem::Dependency
48
56
  name: factory_girl
49
- requirement: &70344345468720 !ruby/object:Gem::Requirement
50
- none: false
57
+ requirement: !ruby/object:Gem::Requirement
51
58
  requirements:
52
- - - ! '>='
59
+ - - ">="
53
60
  - !ruby/object:Gem::Version
54
61
  version: '0'
55
62
  type: :development
56
63
  prerelease: false
57
- version_requirements: *70344345468720
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: bundler
60
- requirement: &70344345468040 !ruby/object:Gem::Requirement
61
- none: false
71
+ requirement: !ruby/object:Gem::Requirement
62
72
  requirements:
63
- - - ! '>='
73
+ - - ">="
64
74
  - !ruby/object:Gem::Version
65
75
  version: '0'
66
76
  type: :development
67
77
  prerelease: false
68
- version_requirements: *70344345468040
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rake
71
- requirement: &70344345467620 !ruby/object:Gem::Requirement
72
- none: false
85
+ requirement: !ruby/object:Gem::Requirement
73
86
  requirements:
74
- - - ! '>='
87
+ - - ">="
75
88
  - !ruby/object:Gem::Version
76
89
  version: '0'
77
90
  type: :development
78
91
  prerelease: false
79
- version_requirements: *70344345467620
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
80
97
  description: This simple gem will help you with keeping default or primary record
81
98
  (one record that is marked as primary) - ie. default domain or language for site
82
99
  email:
@@ -85,35 +102,32 @@ executables: []
85
102
  extensions: []
86
103
  extra_rdoc_files: []
87
104
  files:
88
- - lib/primary/version.rb
89
- - lib/primary.rb
90
- - lib/tasks/primary_tasks.rake
91
105
  - MIT-LICENSE
92
- - Rakefile
93
106
  - README.md
107
+ - Rakefile
108
+ - lib/primary.rb
109
+ - lib/primary/version.rb
110
+ - lib/tasks/primary_tasks.rake
94
111
  homepage: https://github.com/prograils/primary
95
112
  licenses: []
113
+ metadata: {}
96
114
  post_install_message:
97
115
  rdoc_options: []
98
116
  require_paths:
99
117
  - lib
100
118
  required_ruby_version: !ruby/object:Gem::Requirement
101
- none: false
102
119
  requirements:
103
- - - ! '>='
120
+ - - ">="
104
121
  - !ruby/object:Gem::Version
105
122
  version: '0'
106
123
  required_rubygems_version: !ruby/object:Gem::Requirement
107
- none: false
108
124
  requirements:
109
- - - ! '>='
125
+ - - ">="
110
126
  - !ruby/object:Gem::Version
111
127
  version: '0'
112
128
  requirements: []
113
- rubyforge_project:
114
- rubygems_version: 1.8.10
129
+ rubygems_version: 3.0.3
115
130
  signing_key:
116
- specification_version: 3
131
+ specification_version: 4
117
132
  summary: Simple gem that will help with primary/default records
118
133
  test_files: []
119
- has_rdoc: