valuable 0.9.10 → 0.9.11

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: 9bbc18a2b139f32386397c81db9c218fb26c6d31
4
- data.tar.gz: b8fc0a4cdf580799818d8bfa9c012e2c8f2991ea
3
+ metadata.gz: 2a60fe8e5d0483aaf30741b88d9402e5953bb880
4
+ data.tar.gz: 3563930067b4046a967e1c295504f71ac80ed2c4
5
5
  SHA512:
6
- metadata.gz: 943a75a3b565042af7171d14e78264076cb27af3e71c3d263dae2e5946a0009fad12995aba3e44a83c34e1d5325e3e340370eac68221c6b96db159bc23ce941b
7
- data.tar.gz: 2cf6bc7310fbcc1c2b6c4d8932eebd27f22f1acac12b68a88220cc7cb9559f6b8747df66256b60fbefa96624ec4a3cf91952603d3051212950d4498c75bb8aaf
6
+ metadata.gz: d934ef17cc0bb98b12684e9d23e41adc4320898683d03f70170c52b5b38d56ce676c0608c2574c8db4a8e140c681c54b638468294e2299c4f5db3214ab08c48b
7
+ data.tar.gz: a73e26d8ffe8c50e1d971e7c5ec8c95a1708ae33abada1d2eedfdd18bbbc4099555999045fdbd53bc4acce43f88b84f4dc108641a95f768a0816f3ce581f0daf
@@ -0,0 +1,57 @@
1
+ class CustomerSearch < Valuable
2
+ # BE AWARE OF SQL INJECTION!!!
3
+
4
+ has_value :last_name
5
+ has_value :first_name
6
+ has_value :zipcode
7
+ has_value :partner_id, :klass => :integer
8
+
9
+ def terms
10
+ # With truly simple cases, you can just use `attributes` for this
11
+
12
+ terms = {}
13
+ terms[:zipcode] = self.zipcode
14
+ terms[:last_name_like] = "%#{self.last_name}%" if self.last_name
15
+ terms[:first_name_like] = "%#{self.first_name}%" if self.first_name
16
+ terms[:partner_id] = self.partner_id if self.partner_id
17
+ terms
18
+ end
19
+
20
+ def joins
21
+ out = []
22
+ out << [:location] if self.zipcode
23
+ out << [:identifiers] if self.partner_id
24
+ out
25
+ end
26
+
27
+ def conditions
28
+ out = []
29
+
30
+ unless self.last_name.blank?
31
+ out << "customers.last_name like :last_name_like"
32
+ end
33
+
34
+ unless self.first_name.blank?
35
+ out << "customers.first_name like :first_name_like";
36
+ end
37
+
38
+ unless self.zipcode.blank?
39
+ out << "locations.zipcode = :zipcode"
40
+ end
41
+
42
+ unless self.partner_id.blank?
43
+ out << "customer_identifiers.partner_id = :partner_id"
44
+ end
45
+
46
+ if( out.not.empty? )
47
+ [out.join(' and '), terms]
48
+ else
49
+ nil
50
+ end
51
+ end
52
+
53
+ def results
54
+ Customer.joins(joins).where(conditions).includes([:location]).order('customers.id
55
+ desc')
56
+ end
57
+ end
@@ -0,0 +1,29 @@
1
+ class CustomerHistorySearch < Valuable
2
+ has_value :customer_id, klass: :integer
3
+ has_value :client_id, klass: :integer
4
+
5
+ def results
6
+ if client_id && customer_id
7
+ (
8
+ ServiceOrder.where(
9
+ customer_id: customer_id,
10
+ client_id: client_id
11
+ ) +
12
+ SalesOrder.where(
13
+ customer_id: customer_id,
14
+ client_id: client_id
15
+ )
16
+ ).sort_by(&:created_at)
17
+ elsif customer_id
18
+ (
19
+ ServiceOrder.where(
20
+ customer_id: customer_id
21
+ ) +
22
+ PreQ.where(
23
+ customer_id: customer_id
24
+ )
25
+ ).sort_by(&:created_at)
26
+ end
27
+ end
28
+ end
29
+
@@ -158,6 +158,7 @@ class Valuable
158
158
  Valuable::Utils.check_options_validity(self.class.name, name, options)
159
159
 
160
160
  options[:extend] = [options[:extend]].flatten.compact
161
+ options[:allow_blank] = options.has_key?(:allow_blank) ? options[:allow_blank] : true
161
162
 
162
163
  name = name.to_sym
163
164
  _attributes[name] = options
@@ -167,7 +168,7 @@ class Valuable
167
168
  create_question_for(name) if options[:klass] == :boolean
168
169
  create_negative_question_for(name, options[:negative]) if options[:klass] == :boolean && options[:negative]
169
170
 
170
- create_setter_for(name)
171
+ create_setter_for(name, allow_blank: options[:allow_blank] )
171
172
 
172
173
  sudo_alias options[:alias], name if options[:alias]
173
174
  sudo_alias "#{options[:alias]}=", "#{name}=" if options[:alias]
@@ -188,13 +189,14 @@ class Valuable
188
189
  # >> player.attributes[:phone] = "8778675309"
189
190
  # >> player.phone
190
191
  # => "8778675309"
191
- def create_setter_for(attribute)
192
+ def create_setter_for(attribute, options)
192
193
  setter_method = "#{attribute}="
193
194
 
194
195
  define_method setter_method do |value|
195
- write_attribute(attribute, value)
196
+ if options[:allow_blank] || value != ""
197
+ write_attribute(attribute, value)
198
+ end
196
199
  end
197
-
198
200
  end
199
201
 
200
202
  def sudo_alias( alias_name, method_name )
@@ -289,7 +291,7 @@ class Valuable
289
291
  _attributes[name] = options
290
292
 
291
293
  create_accessor_for(name, options[:extend])
292
- create_setter_for(name)
294
+ create_setter_for(name, allow_blank: false)
293
295
 
294
296
  sudo_alias options[:alias], name if options[:alias]
295
297
  sudo_alias "#{options[:alias]}=", "#{name}=" if options[:alias]
@@ -114,7 +114,7 @@ module Valuable::Utils
114
114
  end
115
115
 
116
116
  def known_options
117
- [:klass, :default, :negative, :alias, :parse_with, :extend]
117
+ [:klass, :default, :negative, :alias, :parse_with, :extend, :allow_blank]
118
118
  end
119
119
 
120
120
  # this helper raises an exception if the options passed to has_value
@@ -61,5 +61,13 @@ class TypicalTest < Test::Unit::TestCase
61
61
  def test_that_it_uses_the_default_collection
62
62
  assert_equal Person.new.dreams, [:happiness, :respect]
63
63
  end
64
+
65
+ def test_that_we_can_prevent_blanks
66
+ device = Class.new(Valuable)
67
+ device.has_value( :battery_percent, :allow_blank => false )
68
+
69
+ cell = device.new(:battery_percent => '')
70
+ assert_equal( nil, cell.battery_percent )
71
+ end
64
72
  end
65
73
 
@@ -1 +1 @@
1
- 0.9.10
1
+ 0.9.11
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: valuable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.10
4
+ version: 0.9.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johnathon Wright
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-13 00:00:00.000000000 Z
11
+ date: 2016-04-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Valuable is a ruby base class that is essentially attr_accessor on steroids.
14
14
  A simple and intuitive interface allows you to get on with modeling in your app.
@@ -28,6 +28,8 @@ files:
28
28
  - examples/person.rb
29
29
  - examples/phone_number.rb
30
30
  - examples/rails_presenter.rb
31
+ - examples/search-medium.rb
32
+ - examples/search-simple.rb
31
33
  - lib/valuable.rb
32
34
  - lib/valuable/utils.rb
33
35
  - test/alias_test.rb