constantizable 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/constantizable.rb +23 -14
- data/lib/constantizable/version.rb +1 -1
- 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: 31e80e2702e464a8cc1da077674ae8eea3aa43f7
|
4
|
+
data.tar.gz: a17d53da514af488d34f0cc72db7f33bcff40e83
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 81cc2d45f3e07934a6133a65ab97fcfb12a62520fc690cd4cb5be15085fb68e4ef54882de49d77c42ff0ba0d5bb1d20b4a6cbd6cf6ab4af2e6a0f9d68e9b4b34
|
7
|
+
data.tar.gz: 079cb4f33c60d3f948a6206800af34e64630ff78410b6f4d559f55f57eb67cecfe688835325b8876e565d5f5250d1e4e604187d55e551572cdd6086acfbad972
|
data/lib/constantizable.rb
CHANGED
@@ -4,15 +4,15 @@ module Constantizable
|
|
4
4
|
# like, `ClassMethods` and to include it into `ActiveRecord::Base`.
|
5
5
|
|
6
6
|
module ClassMethods
|
7
|
-
def constantize_column(
|
7
|
+
def constantize_column(*columns)
|
8
8
|
# This method is set as a class method as it can directly be invoked from model definitions
|
9
9
|
# as follows.
|
10
10
|
|
11
11
|
# Class Country < ActiveRecord::Base
|
12
|
-
#
|
12
|
+
# constantize_columns :name, :code
|
13
13
|
# end
|
14
14
|
|
15
|
-
@
|
15
|
+
@constantized_columns = columns
|
16
16
|
end
|
17
17
|
|
18
18
|
private
|
@@ -24,8 +24,8 @@ module Constantizable
|
|
24
24
|
# If Column name isn't present it should fallback to the default `method_missing`
|
25
25
|
# implementation.
|
26
26
|
|
27
|
-
|
28
|
-
super if
|
27
|
+
column_names = @constantized_columns
|
28
|
+
super if column_names.blank?
|
29
29
|
|
30
30
|
# The value of the constantized column needs to be titleized or underscored,
|
31
31
|
# for the implementation to work.
|
@@ -36,10 +36,11 @@ module Constantizable
|
|
36
36
|
# Country.india.
|
37
37
|
# Country with name "united_kingdom", will correspond to the query,
|
38
38
|
# Country.united_kingdom.
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
39
|
+
record = nil
|
40
|
+
column_names.each do | column_name |
|
41
|
+
break if record.present?
|
42
|
+
record = self.find_by("lower(#{column_name}) = ? or lower(#{column_name}) = ?", method.to_s.downcase, method.to_s.titleize.downcase)
|
43
|
+
end
|
43
44
|
|
44
45
|
if record.present?
|
45
46
|
record
|
@@ -60,14 +61,22 @@ module Constantizable
|
|
60
61
|
if method[-1] == '?'
|
61
62
|
# If Column name isn't present it should fallback to the default `method_missing`
|
62
63
|
# implementation.
|
63
|
-
|
64
|
-
|
65
|
-
super if
|
64
|
+
m = method.to_s.gsub("?","")
|
65
|
+
column_names = self.class.instance_variable_get(:@constantized_columns)
|
66
|
+
super if column_names.blank?
|
66
67
|
|
67
68
|
# The value of the constantized column needs to be titleized or underscored.
|
69
|
+
record = nil
|
70
|
+
column_names.each do | column_name |
|
71
|
+
break if record.present?
|
72
|
+
record = self.class.find_by("lower(#{column_name}) = ? or lower(#{column_name}) = ?", m.to_s.downcase, m.to_s.titleize.downcase)
|
73
|
+
end
|
68
74
|
|
69
|
-
|
70
|
-
|
75
|
+
if record.present?
|
76
|
+
self.id == record.id
|
77
|
+
else
|
78
|
+
super
|
79
|
+
end
|
71
80
|
else
|
72
81
|
super
|
73
82
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: constantizable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Abhishek Sarkar
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2014-
|
13
|
+
date: 2014-10-20 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rails
|