uniqable 0.1.3 → 0.2.0

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: b68f76b3b88795ba68de78138fd7aca5afe838f0
4
- data.tar.gz: c3f5d75e44f0f96c532d4a6c73df7986f8921fec
3
+ metadata.gz: 614ad5000574685a9eae9222e26c04898a03b22e
4
+ data.tar.gz: ff0f29efadbf39c75d7b2340af4a3cf628fc3e78
5
5
  SHA512:
6
- metadata.gz: 3764a994583ea2e219f746f55671e224ea58ed0bd9cdb710621d780f064771bbfd4f88ffd7a8cdfc8dbb742e04c989b52225f52b212b73b20fe70805654b95f7
7
- data.tar.gz: 6d337e6ef441ac0a7db557aea799fea790c8b3ba40e2bbc279ac57b78ed1fe3f2aa0a23be731b5ac9701bbb5dc1892634ff8230d569ed0666b2db50eef143a25
6
+ metadata.gz: ae466818b9f47c24b1a9a0d5359f37cbde6e189a6ac602c72204d585778323b75a32b02ffa97bbca00ec20f08f8ea7c1284936463043659801ec45dd832a38e0
7
+ data.tar.gz: c34d784116d1c532248f3aed6884a4ddc5cd8008f2fe2d08ce81f8ce67c3ec93c800fc963ff7b452d635c0ab34521878be3cf12b0e7c477304a1c2fa01d586ef
data/Gemfile CHANGED
@@ -9,4 +9,4 @@ gemspec
9
9
 
10
10
  group :test do
11
11
  gem 'rspec_junit_formatter'
12
- end
12
+ end
@@ -21,6 +21,7 @@ GEM
21
21
  tzinfo (~> 1.1)
22
22
  arel (5.0.1.20140414130214)
23
23
  ast (2.4.0)
24
+ awesome_print (1.8.0)
24
25
  builder (3.2.3)
25
26
  concurrent-ruby (1.0.5)
26
27
  diff-lcs (1.3)
@@ -57,6 +58,7 @@ GEM
57
58
  ruby-progressbar (~> 1.7)
58
59
  unicode-display_width (~> 1.0, >= 1.0.1)
59
60
  ruby-progressbar (1.9.0)
61
+ sqlite3 (1.3.13)
60
62
  thread_safe (0.3.6)
61
63
  tzinfo (1.2.5)
62
64
  thread_safe (~> 0.1)
@@ -67,11 +69,13 @@ PLATFORMS
67
69
 
68
70
  DEPENDENCIES
69
71
  activerecord (~> 4.1.14.1)
72
+ awesome_print
70
73
  bundler (~> 1.16)
71
74
  rake (~> 10.0)
72
75
  rspec (~> 3.0)
73
76
  rspec_junit_formatter
74
77
  rubocop (~> 0.49)
78
+ sqlite3
75
79
  uniqable!
76
80
 
77
81
  BUNDLED WITH
data/README.md CHANGED
@@ -41,6 +41,8 @@ if you want you can use `:to_param` option to generate automatically `#to_param`
41
41
  anyway you have one more method `.find_uniqable` which one you can use to find your model record
42
42
  ```ruby
43
43
  MyModel.find_uniqable params[:uid]
44
+ # or
45
+ MyModel.find_uniqable! params[:uid] # this will raise exception ActiveRecord::RecordNotFound if record is absent
44
46
  ```
45
47
  and another one is `.uniqable_fields`
46
48
  ```ruby
@@ -8,10 +8,12 @@ module Uniqable
8
8
  base.extend ClassMethods
9
9
  end
10
10
 
11
+ # Methods going to be AR model's methods
11
12
  module ClassMethods
12
13
  # Uniqable fields and options declaration
13
14
  # @example:
14
15
  # uniqable :uid, :slug, to_param: :uid
16
+ # rubocop:disable Metrics/MethodLength
15
17
  def uniqable(*fields, to_param: nil)
16
18
  fields = [:uid] if fields.blank?
17
19
  fields.each do |name|
@@ -21,12 +23,18 @@ module Uniqable
21
23
  fields
22
24
  end
23
25
 
24
- if to_param # :to_param option
25
- define_method :to_param do
26
- public_send(to_param)
27
- end
26
+ return if to_param.blank? # :to_param option
27
+ define_method :to_param do
28
+ public_send(to_param)
28
29
  end
29
30
  end
31
+ # rubocop:enable Metrics/MethodLength
32
+
33
+ # @return [self]
34
+ def where_uniqable(uid)
35
+ where_sql = uniqable_fields.map { |r| "#{table_name}.#{r} = :uid" }.join(' OR ')
36
+ where(where_sql, uid: uid)
37
+ end
30
38
 
31
39
  # Find record by one of the uniq field
32
40
  # usage @example:
@@ -35,8 +43,13 @@ module Uniqable
35
43
  # MyModel.find_uniqable params[:uid] # can be uid or slug column
36
44
  # @return [self]
37
45
  def find_uniqable(uid)
38
- where_sql = uniqable_fields.map{ |r| "#{table_name}.#{r} = :uid"}.join(' OR ')
39
- self.where(where_sql, uid: uid).take
46
+ where_uniqable(uid).take
47
+ end
48
+
49
+ # Same as method above just raise exception if nothing is there
50
+ # @return [self]
51
+ def find_uniqable!(uid)
52
+ where_uniqable(uid).take!
40
53
  end
41
54
  end
42
55
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Uniqable
4
- VERSION = '0.1.3'
4
+ VERSION = '0.2.0'
5
5
  end
@@ -23,8 +23,10 @@ Gem::Specification.new do |spec|
23
23
  spec.require_paths = ['lib']
24
24
 
25
25
  spec.add_development_dependency 'activerecord', '~> 4.1.14.1'
26
+ spec.add_development_dependency 'awesome_print'
26
27
  spec.add_development_dependency 'bundler', '~> 1.16'
27
28
  spec.add_development_dependency 'rake', '~> 10.0'
28
29
  spec.add_development_dependency 'rspec', '~> 3.0'
29
30
  spec.add_development_dependency 'rubocop', '~> 0.49'
31
+ spec.add_development_dependency 'sqlite3'
30
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uniqable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Renat "MpaK" Ibragimov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-06-08 00:00:00.000000000 Z
11
+ date: 2018-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 4.1.14.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: awesome_print
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +94,20 @@ dependencies:
80
94
  - - "~>"
81
95
  - !ruby/object:Gem::Version
82
96
  version: '0.49'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sqlite3
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
83
111
  description: Generate a unique, random token for ActiveRecord models.
84
112
  email:
85
113
  - mrak69@gmail.com