effective_resources 0.2.4 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b55e4948a32a2526cc0ce43230a6bee620c09a14
4
- data.tar.gz: d0b8bf59119514d156e4b63428cc068960cb779e
3
+ metadata.gz: c8670335e749c4dfd51af3948b37cbdb7b4051ee
4
+ data.tar.gz: 82ee3513ad299bee24a8bf6e76893224f2fb85d9
5
5
  SHA512:
6
- metadata.gz: 508301a27e166a14290d8085066c773d584ab2e39c294106b7361d973219712f359e227178904f7f58f9849e551204d317d042d49de594f85c2417e896f818e4
7
- data.tar.gz: c96b6875d7685e62b288fef2e8892eb7a1c664cfd6839ac0e491e876e103224e75d1fcced5ba2c83d427ec98322767f61fb99915ebc4f3bfd18b0da0aee9cfe5
6
+ metadata.gz: e96e5c5b6b876e9389b44d6778958200b363d29d16a70a628c5c9ef1eb1eaad9f2349aa84cd92eb0757ce1fafc7e8c107352c9f1b6ea225d6a405c547121100e
7
+ data.tar.gz: 076396560c96b7c4d9951dc3ceff5a2864ac749703d604b6d1e9a09c5d41838a96eca592f832d1b5b2fb148c43a60c0857dfda64cfbae879df0973bf3a6ec83a
@@ -25,7 +25,7 @@ module Effective
25
25
  self.resources ||= resource_class.all
26
26
 
27
27
  if resource_datatable_class
28
- @datatable ||= resource_datatable_class.new(resource_datatable_attributes, params[:scopes])
28
+ @datatable ||= resource_datatable_class.new(self, resource_datatable_attributes)
29
29
  end
30
30
  end
31
31
 
@@ -17,4 +17,59 @@ module EffectiveResourcesHelper
17
17
  end
18
18
  end
19
19
 
20
+ def number_to_duration(duration)
21
+ duration = duration.to_i
22
+ value = duration.abs
23
+
24
+ [
25
+ ('-' if duration < 0),
26
+ ("#{value / 60}h " if value >= 60),
27
+ ("#{'%0.2d' % (value % 60)}m" if value > 0),
28
+ ('0m' if value == 0),
29
+ ].compact.join
30
+ end
31
+
32
+ ### Icon Helpers for actions_column or elsewhere
33
+ def show_icon_to(path, options = {})
34
+ glyphicon_to('eye-open', path, {title: 'Show'}.merge(options))
35
+ end
36
+
37
+ def edit_icon_to(path, options = {})
38
+ glyphicon_to('edit', path, {title: 'Edit'}.merge(options))
39
+ end
40
+
41
+ def destroy_icon_to(path, options = {})
42
+ defaults = {title: 'Destroy', data: {method: :delete, confirm: 'Delete this item?'}}
43
+ glyphicon_to('trash', path, defaults.merge(options))
44
+ end
45
+
46
+ def settings_icon_to(path, options = {})
47
+ glyphicon_to('cog', path, {title: 'Settings'}.merge(options))
48
+ end
49
+
50
+ def ok_icon_to(path, options = {})
51
+ glyphicon_to('ok', path, {title: 'OK'}.merge(options))
52
+ end
53
+
54
+ def approve_icon_to(path, options = {})
55
+ glyphicon_to('ok', path, {title: 'Approve'}.merge(options))
56
+ end
57
+
58
+ def remove_icon_to(path, options = {})
59
+ glyphicon_to('remove', path, {title: 'Remove'}.merge(options))
60
+ end
61
+
62
+ def glyphicon_to(icon, path, options = {})
63
+ content_tag(:a, options.merge(href: path)) do
64
+ if icon.start_with?('glyphicon-')
65
+ content_tag(:span, '', class: "glyphicon #{icon}")
66
+ else
67
+ content_tag(:span, '', class: "glyphicon glyphicon-#{icon}")
68
+ end
69
+ end
70
+ end
71
+ alias_method :bootstrap_icon_to, :glyphicon_to
72
+ alias_method :glyph_icon_to, :glyphicon_to
73
+
74
+
20
75
  end
@@ -26,21 +26,28 @@ module Effective
26
26
 
27
27
  @type ||= (
28
28
  case obj
29
- when :boolean ; :boolean
30
- when :date ; :date
31
- when :datetime ; :datetime
32
- when :decimal ; :decimal
33
- when :integer ; :integer
34
- when :price ; :price
35
- when :nil ; :nil
36
- when :string ; :string
37
- when FalseClass ; :boolean
38
- when Fixnum ; :integer
39
- when Float ; :decimal
40
- when NilClass ; :nil
41
- when String ; :string
42
- when TrueClass ; :boolean
29
+ when :boolean ; :boolean
30
+ when :currency ; :currency
31
+ when :date ; :date
32
+ when :datetime ; :datetime
33
+ when :decimal ; :decimal
34
+ when :duration ; :duration
35
+ when :email ; :email
36
+ when :integer ; :integer
37
+ when :percentage ; :percentage
38
+ when :price ; :price
39
+ when :nil ; :nil
40
+ when :resource ; :resource
41
+ when :string ; :string
42
+ when :text ; :text
43
+ when FalseClass ; :boolean
44
+ when Fixnum ; :integer
45
+ when Float ; :decimal
46
+ when NilClass ; :nil
47
+ when String ; :string
48
+ when TrueClass ; :boolean
43
49
  when ActiveSupport::TimeWithZone ; :datetime
50
+ when ActiveRecord::Base ; :resource
44
51
  when :belongs_to ; :belongs_to
45
52
  when :belongs_to_polymorphic ; :belongs_to_polymorphic
46
53
  when :has_many ; :has_many
@@ -50,7 +57,7 @@ module Effective
50
57
  when :effective_obfuscation ; :effective_obfuscation
51
58
  when :effective_roles ; :effective_roles
52
59
  else
53
- raise 'unsupported type'
60
+ raise "unsupported type for #{obj}"
54
61
  end
55
62
  )
56
63
  end
@@ -62,36 +69,41 @@ module Effective
62
69
  when :date, :datetime
63
70
  date = Time.zone.local(*value.to_s.scan(/(\d+)/).flatten)
64
71
  name.to_s.start_with?('end_') ? date.end_of_day : date
65
- when :decimal
66
- (value.kind_of?(String) ? value.gsub(/[^0-9|\.]/, '') : value).to_f
72
+ when :decimal, :currency
73
+ (value.kind_of?(String) ? value.gsub(/[^0-9|\-|\.]/, '') : value).to_f
74
+ when :duration
75
+ if value.to_s.include?('h')
76
+ (hours, minutes) = (value.to_s.gsub(/[^0-9|\-|h]/, '').split('h'))
77
+ (hours.to_i * 60) + ((hours.to_i < 0) ? -(minutes.to_i) : minutes.to_i)
78
+ else
79
+ value.to_s.gsub(/[^0-9|\-|h]/, '').to_i
80
+ end
67
81
  when :effective_obfuscation
68
82
  klass.respond_to?(:deobfuscate) ? klass.deobfuscate(value) : value.to_s
69
83
  when :effective_roles
70
- EffectiveRoles.roles_for(value)
71
- when :integer
84
+ EffectiveRoles.roles.include?(value.to_sym) ? value : EffectiveRoles.roles_for(value)
85
+ when :integer, :percentage
72
86
  (value.kind_of?(String) ? value.gsub(/\D/, '') : value).to_i
73
87
  when :nil
74
88
  value.presence
75
89
  when :price
76
- (value.kind_of?(Integer) ? value : (value.to_s.gsub(/[^0-9|\.]/, '').to_f * 100.0)).to_i
77
- when :string
90
+ (value.kind_of?(Integer) ? value : (value.to_s.gsub(/[^0-9|\-|\.]/, '').to_f * 100.0)).to_i
91
+ when :string, :text, :email
78
92
  value.to_s
79
93
  when :belongs_to_polymorphic
80
94
  value.to_s
81
- when :belongs_to, :has_many, :has_and_belongs_to_many, :has_one # Returns an Array of ints, an Int or String
95
+ when :belongs_to, :has_many, :has_and_belongs_to_many, :has_one, :resource # Returns an Array of ints, an Int or String
82
96
  if value.kind_of?(Integer) || value.kind_of?(Array)
83
97
  value
84
98
  else
85
99
  digits = value.to_s.gsub(/[^0-9|,]/, '') # '87' or '87,254,300' or 'something'
86
100
 
87
- if digits == value && digits.index(',').present?
101
+ if digits == value || digits.length == 10
88
102
  if klass.respond_to?(:deobfuscate)
89
103
  digits.split(',').map { |str| klass.deobfuscate(str).to_i }
90
104
  else
91
105
  digits.split(',').map { |str| str.to_i }
92
106
  end
93
- elsif digits == value
94
- klass.respond_to?(:deobfuscate) ? klass.deobfuscate(digits).to_i : digits.to_i
95
107
  else
96
108
  value.to_s
97
109
  end
@@ -61,7 +61,7 @@ module Effective
61
61
 
62
62
  def has_one(name)
63
63
  name = name.to_sym
64
- has_ones.find { |ho| ass.name == name }
64
+ has_ones.find { |ass| ass.name == name }
65
65
  end
66
66
 
67
67
  def nested_resource(name)
@@ -6,15 +6,15 @@ module Effective
6
6
  def search_form_field(name, type = nil)
7
7
  case (type || sql_type(name))
8
8
  when :belongs_to
9
- { as: :select }.merge(associated_search_collection(belongs_to(name)))
9
+ { as: :select }.merge(search_form_field_collection(belongs_to(name)))
10
10
  when :belongs_to_polymorphic
11
11
  { as: :grouped_select, polymorphic: true, collection: nil}
12
12
  when :has_and_belongs_to_many
13
- { as: :select }.merge(associated_search_collection(has_and_belongs_to_many(name)))
13
+ { as: :select }.merge(search_form_field_collection(has_and_belongs_to_many(name)))
14
14
  when :has_many
15
- { as: :select, multiple: true }.merge(associated_search_collection(has_many(name)))
15
+ { as: :select, multiple: true }.merge(search_form_field_collection(has_many(name)))
16
16
  when :has_one
17
- { as: :select, multiple: true }.merge(associated_search_collection(has_one(name)))
17
+ { as: :select, multiple: true }.merge(search_form_field_collection(has_one(name)))
18
18
  when :effective_addresses
19
19
  { as: :string }
20
20
  when :effective_roles
@@ -31,15 +31,15 @@ module Effective
31
31
  { as: :number }
32
32
  when :text
33
33
  { as: :text }
34
+ when ActiveRecord::Base
35
+ { as: :select }.merge(Effective::Resource.new(type).search_form_field_collection)
34
36
  else
35
37
  { as: :string }
36
38
  end
37
39
  end
38
40
 
39
- private
40
-
41
- def associated_search_collection(association, max_id = 1000)
42
- res = Effective::Resource.new(association)
41
+ def search_form_field_collection(association = nil, max_id = 1000)
42
+ res = (association.nil? ? self : Effective::Resource.new(association))
43
43
 
44
44
  if res.max_id > max_id
45
45
  { as: :string }
@@ -12,7 +12,7 @@ module Effective
12
12
 
13
13
  def _initialize_input_name(input)
14
14
  case input
15
- when String ; input
15
+ when String ; input.start_with?('/') ? input[1..-1] : input
16
16
  when Symbol ; input
17
17
  when Class ; input.name
18
18
  when ActiveRecord::Relation
@@ -52,7 +52,6 @@ module Effective
52
52
 
53
53
  if first && last
54
54
  @written_attributes = reader.select(from: first+1, to: last-1).map do |line|
55
-
56
55
  Effective::Attribute.parse_written(line).presence
57
56
  end.compact
58
57
  end
@@ -29,6 +29,10 @@ module Effective
29
29
  )
30
30
  end
31
31
 
32
+ def active_record?
33
+ klass.ancestors.include?(ActiveRecord::Base)
34
+ end
35
+
32
36
  end
33
37
  end
34
38
  end
@@ -33,9 +33,12 @@ module Effective
33
33
  .order("#{sql_column(klass.primary_key)} #{sql_direction}")
34
34
  when :effective_roles
35
35
  relation.order("#{sql_column(:roles_mask)} #{sql_direction}")
36
+ when :string, :text
37
+ relation
38
+ .order((("ISNULL(#{sql_column}}), ") if mysql?).to_s + "#{sql_column}='' ASC, #{sql_column} #{sql_direction}" + (" NULLS LAST" if postgres?))
36
39
  else
37
40
  relation
38
- .order("#{sql_column} #{sql_direction}")
41
+ .order((("ISNULL(#{sql_column}}), ") if mysql?).to_s + "#{sql_column} #{sql_direction}" + (" NULLS LAST" if postgres?))
39
42
  end
40
43
  end
41
44
 
@@ -81,13 +84,33 @@ module Effective
81
84
  end
82
85
  )
83
86
  relation.where("#{sql_column} >= ? AND #{sql_column} <= ?", term, end_at)
84
- when :decimal
85
- relation.where("#{sql_column} = ?", term)
87
+ when :decimal, :currency
88
+ if fuzzy && (term.round(0) == term) && value.to_s.include?('.') == false
89
+ if term < 0
90
+ relation.where("#{sql_column} <= ? AND #{sql_column} > ?", term, term-1.0)
91
+ else
92
+ relation.where("#{sql_column} >= ? AND #{sql_column} < ?", term, term+1.0)
93
+ end
94
+ else
95
+ relation.where("#{sql_column} = ?", term)
96
+ end
97
+ when :duration
98
+ if fuzzy && (term % 60) == 0 && value.to_s.include?('m') == false
99
+ if term < 0
100
+ relation.where("#{sql_column} <= ? AND #{sql_column} > ?", term, term-60)
101
+ else
102
+ relation.where("#{sql_column} >= ? AND #{sql_column} < ?", term, term+60)
103
+ end
104
+ else
105
+ relation.where("#{sql_column} = ?", term)
106
+ end
86
107
  when :integer
87
108
  relation.where("#{sql_column} = ?", term)
109
+ when :percentage
110
+ relation.where("#{sql_column} = ?", term)
88
111
  when :price
89
112
  relation.where("#{sql_column} = ?", term)
90
- when :string, :text
113
+ when :string, :text, :email
91
114
  if fuzzy
92
115
  relation.where("#{sql_column} #{ilike} ?", "%#{term}%")
93
116
  else
@@ -102,7 +125,7 @@ module Effective
102
125
  raise 'expected relation to be present' unless relation
103
126
 
104
127
  # Assume this is a set of IDs
105
- if value.kind_of?(Integer) || value.kind_of?(Array)
128
+ if value.kind_of?(Integer) || value.kind_of?(Array) || (value.to_i.to_s == value)
106
129
  return relation.where(klass.primary_key => value)
107
130
  end
108
131
 
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '0.2.4'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-10 00:00:00.000000000 Z
11
+ date: 2017-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails