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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a14d1a27b451918b97c68bb2e22bc4a66d460a5e
4
- data.tar.gz: d56a6c3559c2185dcbce35f44152965c44d3c262
3
+ metadata.gz: 31e80e2702e464a8cc1da077674ae8eea3aa43f7
4
+ data.tar.gz: a17d53da514af488d34f0cc72db7f33bcff40e83
5
5
  SHA512:
6
- metadata.gz: 785d69c96c41ac2d0681ae6a24b986d417eeb0fe3b9a7251720bf13f0ef2da9df68fc7008fdf27e76ec84d49e1d11713187cf9602a177592a1783496383fda3f
7
- data.tar.gz: 782983fc339112987fd3a3121a7a33b0a65505c15e87b9b2fc2de6825dbedd7bf15641f998932ff3497752a7ff74c71dea3d225b2f549ced41c3a2fe8d2cc8f6
6
+ metadata.gz: 81cc2d45f3e07934a6133a65ab97fcfb12a62520fc690cd4cb5be15085fb68e4ef54882de49d77c42ff0ba0d5bb1d20b4a6cbd6cf6ab4af2e6a0f9d68e9b4b34
7
+ data.tar.gz: 079cb4f33c60d3f948a6206800af34e64630ff78410b6f4d559f55f57eb67cecfe688835325b8876e565d5f5250d1e4e604187d55e551572cdd6086acfbad972
@@ -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(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
- # constantize_column :name
12
+ # constantize_columns :name, :code
13
13
  # end
14
14
 
15
- @constantized_column = column
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
- column_name = @constantized_column
28
- super if column_name.nil?
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
- record = (
41
- self.find_by("#{column_name} = ? or #{column_name} = ?", method.to_s, method.to_s.titleize)
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
- column_name = self.class.instance_variable_get(:@constantized_column)
65
- super if column_name.nil?
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
- data = self.send(column_name)
70
- method[0..-2] == data.titleize.tr(' ','').underscore
75
+ if record.present?
76
+ self.id == record.id
77
+ else
78
+ super
79
+ end
71
80
  else
72
81
  super
73
82
  end
@@ -1,3 +1,3 @@
1
1
  module Constantizable
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  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.1.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-09-27 00:00:00.000000000 Z
13
+ date: 2014-10-20 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails