amountable 0.2.0 → 0.2.1

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: 70fd4bd67ff8f0f3c0a67ea5701fb035934c3a8f
4
- data.tar.gz: 3a7f3ce3ff221dcbe13378f3ebe18958385e787d
3
+ metadata.gz: d7fb532f0098d76632e7fbc2f46ad8a79570f798
4
+ data.tar.gz: df59859c26e14e795e38243b4a097059fccdffff
5
5
  SHA512:
6
- metadata.gz: b785e2e1a02a280a3e6125c4e3731b1281eb02760a17405f669e1d1669d089131a3c9e01cc084ffa2bc6673ca3721dcc5060c974e892aaad973d2deb2d3097a8
7
- data.tar.gz: 3063523fcfa424dc576bdb2ad3cf7a2e2eca9378098cde60a06fbb150d40c8a6313ab7e4cd77f6a6c51e04ed9ecc8b1c0b3d2a38e9a849fc80fb51a3c96b32f3
6
+ metadata.gz: d1b982b2a5d90e3c28ad0905ba0154c6f2c625681a989180743cc88dd0853918186330b91afe4f05fe7c13eae7a58d7241a38e6e920053b46d4fb3737ca2563b
7
+ data.tar.gz: 54a8b7d3ff9df4312b1dae3a8bc71a8a1fc063aba18ac71f055255d1b834531644b9e377657dfae7274908dafc04eacbd5872146af46139a7e05fc9a5c59c8db
data/lib/amountable.rb CHANGED
@@ -59,10 +59,12 @@ module Amountable
59
59
  class_attribute :amount_names
60
60
  class_attribute :amount_sets
61
61
  class_attribute :amounts_column_name
62
+ class_attribute :storage
62
63
  self.amount_sets = Hash.new { |h, k| h[k] = Set.new }
63
64
  self.amount_names = Set.new
64
65
  self.amounts_column_name = 'amounts'
65
- case (options[:storage] || :table).to_sym
66
+ self.storage = (options[:storage] || :table).to_sym
67
+ case self.storage
66
68
  when :table
67
69
  has_many :amounts, class_name: 'Amountable::Amount', as: :amountable, dependent: :destroy, autosave: false
68
70
  include Amountable::TableMethods
@@ -105,6 +107,42 @@ module Amountable
105
107
  self.amount_names.include?(name.to_sym)
106
108
  end
107
109
 
110
+ def where(opts, *rest)
111
+ return super unless opts.is_a?(Hash)
112
+ if self.storage == :jsonb
113
+ where_json(opts, *rest)
114
+ else
115
+ super
116
+ end
117
+ end
118
+
119
+ def where_json(opts, *rest)
120
+ values = []
121
+ query = opts.inject([]) do |mem, (column, value)|
122
+ column = column.to_sym
123
+ if column.in?(self.amount_names) || column.in?(self.amount_sets.keys)
124
+ mem << "#{self.pg_json_field_access(column, :cents)} = '%s'"
125
+ mem << "#{self.pg_json_field_access(column, :currency)} = '%s'"
126
+ values << value.to_money.fractional
127
+ values << value.to_money.currency.iso_code
128
+ opts.delete(column)
129
+ end
130
+ mem
131
+ end
132
+ query = [query.join(' AND ')] + values
133
+ where(query, *rest).where(opts, *rest)
134
+ end
135
+
136
+ def pg_json_field_access(name, field = :cents)
137
+ name = name.to_sym
138
+ group = if name.in?(self.amount_names)
139
+ 'amounts'
140
+ elsif name.in?(self.amount_sets.keys)
141
+ 'sets'
142
+ end
143
+ "#{self.amounts_column_name}::json#>>'{#{group},#{name},#{field}}'"
144
+ end
145
+
108
146
  end
109
147
  end
110
148
 
@@ -1,5 +1,5 @@
1
1
  # Copyright 2015-2016, Instacart
2
2
 
3
3
  module Amountable
4
- VERSION = '0.2.0'
4
+ VERSION = '0.2.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amountable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emmanuel Turlay
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-21 00:00:00.000000000 Z
11
+ date: 2016-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord