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, request = nil, &block) ⇒ Object
- .dictionary_name ⇒ Object
- .fields(dictionary_name = :default) ⇒ Object
- .method_missing(m, *args, &block) ⇒ Object
- .normalize_operators(operators) ⇒ Object
- .normalized_name(op) ⇒ Object
- .operator_default(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
164 165 166 167 |
# File 'lib/visual_condition_builder/dictionary.rb', line 164 def array_hashes_to_hash(array=nil) array ||= [] array.reduce Hash.new, :merge end |
.dictionary(name = :default, request = nil, &block) ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/visual_condition_builder/dictionary.rb', line 12 def dictionary(name=:default, request=nil, &block) (self.dictionaries ||= {})[name] ||= [] @dictionary_name = name block.call if block_given? if request.present? ApplicationDictionary.new(request).send(:dictionary, self.name, name) else self.dictionaries[name] end end |
.dictionary_name ⇒ Object
160 161 162 |
# File 'lib/visual_condition_builder/dictionary.rb', line 160 def dictionary_name self.to_s.sub('Dictionary', '').underscore.to_sym end |
.fields(dictionary_name = :default) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/visual_condition_builder/dictionary.rb', line 23 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
80 81 82 83 84 85 86 87 88 |
# File 'lib/visual_condition_builder/dictionary.rb', line 80 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 |
.normalized_name(op) ⇒ Object
169 170 171 |
# File 'lib/visual_condition_builder/dictionary.rb', line 169 def normalized_name(op) op.to_s.downcase.to_sym end |
.operator_default(op) ⇒ Object
149 150 151 152 153 154 |
# File 'lib/visual_condition_builder/dictionary.rb', line 149 def operator_default(op) op_default = operators_list(op) operator = {operator: normalized_name(op)} operator.deep_merge!(op_default) if op_default.present? operator end |
.operator_translate(op) ⇒ Object
156 157 158 |
# File 'lib/visual_condition_builder/dictionary.rb', line 156 def operator_translate(op) I18n.t(op, default: op.to_s.humanize, scope: [:condition_builder, :operators]) end |
.operators_by_type(type) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/visual_condition_builder/dictionary.rb', line 61 def operators_by_type(type) type = type.present? ? normalized_name(type) : 'string' operators = case type when :date, :datetime [:eq, :between, :today, :yesterday, :tomorrow, :this_week, :last_week, :next_week, :present, :blank] when :time [:eq, :between, :present, :blank] when :decimal, :integer [:eq, :between] when :boolean [:true, :false, :present, :blank] when :string [:cont, :eq, :start, :end, :present, :blank] else [:eq] end operators.map{|op| operator_default(op) } end |
.operators_list(op = nil) ⇒ Object
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 139 140 141 142 143 144 145 146 147 |
# File 'lib/visual_condition_builder/dictionary.rb', line 90 def operators_list(op=nil) operators = { between: {multiple: 2}, today: {no_value: true}, yesterday: {no_value: true}, tomorrow: {no_value: true}, this_week: {no_value: true}, last_week: {no_value: true}, next_week: {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[normalized_name(op)] || {}) else operators end end |
.param(attr, *args) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/visual_condition_builder/dictionary.rb', line 38 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 normalized_name(args[:type])==:boolean && args[:label] !~ /\?$/ args[:label]+='?' end self.dictionaries[@dictionary_name] << args end |