Class: VisualConditionBuilder::Dictionary
- Inherits:
-
Object
- Object
- VisualConditionBuilder::Dictionary
- Defined in:
- lib/visual_condition_builder/dictionary.rb
Class Method Summary collapse
- .array_hashes_to_hash(array = nil) ⇒ Object
- .dictionary(name = :default, &block) ⇒ Object
- .dictionary_name ⇒ Object
- .fields(dictionary_name = :default) ⇒ Object
- .method_missing(m, *args, &block) ⇒ Object
- .normalize_operators(operators) ⇒ Object
- .operator_default(op) ⇒ Object
- .operator_name(op) ⇒ Object
- .operator_translate(op) ⇒ Object
- .operators_by_type(type) ⇒ Object
- .operators_list(op = nil) ⇒ Object
- .param(attr, *args) ⇒ Object
Class Method Details
.array_hashes_to_hash(array = nil) ⇒ Object
155 156 157 158 |
# File 'lib/visual_condition_builder/dictionary.rb', line 155 def array_hashes_to_hash(array=nil) array ||= [] array.reduce Hash.new, :merge end |
.dictionary(name = :default, &block) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/visual_condition_builder/dictionary.rb', line 12 def dictionary(name=:default, &block) (self.dictionaries ||= {})[name] ||= [] @dictionary_name = name block.call if block_given? self.dictionaries[name] end |
.dictionary_name ⇒ Object
151 152 153 |
# File 'lib/visual_condition_builder/dictionary.rb', line 151 def dictionary_name self.to_s.sub('Dictionary', '').underscore.to_sym end |
.fields(dictionary_name = :default) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/visual_condition_builder/dictionary.rb', line 19 def fields(dictionary_name=:default) dictionary ||= {} self.dictionaries[dictionary_name].group_by{|h| h[:group]}.each do |group, attrs| dictionary[group] ||= {} if group.present? attrs.each do |attr| if group.present? dictionary[group][attr[:field]] = attr.slice(:label, :type) else dictionary[attr[:field]] = attr.slice(:label, :type) end end end dictionary end |
.method_missing(m, *args, &block) ⇒ Object
6 7 8 9 10 |
# File 'lib/visual_condition_builder/dictionary.rb', line 6 def method_missing(m, *args, &block) if m =~ /_url|_path/ Rails.application.routes.url_helpers.send(m, args) end end |
.normalize_operators(operators) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/visual_condition_builder/dictionary.rb', line 73 def normalize_operators(operators) operators.map do |operator| operator = operator_default(operator) unless operator.is_a?(Hash) operator[:no_value] ||= false operator[:multiple] ||= false operator[:label] ||= operator_translate(operator[:operator]) operator end end |
.operator_default(op) ⇒ Object
140 141 142 143 144 145 |
# File 'lib/visual_condition_builder/dictionary.rb', line 140 def operator_default(op) op_default = operators_list(op) operator = {operator: operator_name(op)} operator.deep_merge!(op_default) if op_default.present? operator end |
.operator_name(op) ⇒ Object
160 161 162 |
# File 'lib/visual_condition_builder/dictionary.rb', line 160 def operator_name(op) op.to_s.downcase.to_sym end |
.operator_translate(op) ⇒ Object
147 148 149 |
# File 'lib/visual_condition_builder/dictionary.rb', line 147 def operator_translate(op) I18n.t(op, default: op.to_s.humanize, scope: [:condition_builder, :operators]) end |
.operators_by_type(type) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/visual_condition_builder/dictionary.rb', line 56 def operators_by_type(type) type = type.present? ? type.to_s.downcase.to_sym : 'string' operators = case type when :date, :datetime [:eq, :between, :today, :yesterday, :this_week, :last_week, :present, :blank] when :time [:eq, :between, :present, :blank] when :decimal, :integer [:eq, :between] when :string [:cont, :eq, :start, :end, :present, :blank] else [:eq] end operators.map{|op| operator_default(op) } end |
.operators_list(op = nil) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/visual_condition_builder/dictionary.rb', line 83 def operators_list(op=nil) operators = { between: {multiple: 2}, today: {no_value: true}, yesterday: {no_value: true}, this_week: {no_value: true}, last_wee: {no_value: true}, eq: {multiple: false}, not_eq: {multiple: false}, matches: {multiple: false}, does_not_match: {multiple: false}, lt: {multiple: false}, gt: {multiple: false}, lteq: {multiple: false}, gteq: {multiple: false}, in: {multiple: true}, not_in: {multiple: true}, cont: {multiple: false}, not_cont: {multiple: false}, cont_any: {multiple: true}, not_cont_any: {multiple: true}, cont_all: {multiple: true}, not_cont_all: {multiple: true}, start: {multiple: false}, not_start: {multiple: false}, end: {multiple: false}, not_end: {multiple: false}, true: {no_value: true, multiple: false}, not_true: {no_value: true, multiple: false}, false: {no_value: true, multiple: false}, not_false: {no_value: true, multiple: false}, present: {no_value: true, multiple: false}, blank: {no_value: true, multiple: false}, null: {no_value: true, multiple: false}, not_null: {no_value: true, multiple: false}, } if op.present? (operators[operator_name(op)] || {}) else operators end end |
.param(attr, *args) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/visual_condition_builder/dictionary.rb', line 34 def param(attr, *args) #DEFAULT VALUES args = array_hashes_to_hash(args) args[:type] ||= 'STRING' args[:operators] = operators_by_type(args[:type]) unless args[:operators].present? args[:operators] = normalize_operators(args[:operators]) args[:values] ||= [] args[:group] ||= '' if args[:group].present? && args[:group].is_a?(Symbol) args[:label] ||= I18n.t(attr.to_sym, default: attr.to_s.humanize, scope: [:condition_dictionaries, args[:group]]) args[:field] ||= "#{args[:group]}_#{attr}" args[:group] = {args[:group] => I18n.t(args[:group], default: args[:group].to_s, scope: [:condition_builder, :dictionaries])} else args[:label] ||= I18n.t(attr.to_sym, default: attr.to_s.humanize, scope: [:condition_dictionaries, dictionary_name]) args[:field] ||= attr end if args[:values].present? && args[:values].is_a?(Proc) args[:values] = args[:values].call end self.dictionaries[@dictionary_name] << args end |