Module: Cullender::RulesHelper
- Defined in:
- app/helpers/cullender/rules_helper.rb
Instance Method Summary collapse
- #cullender_collection_and_select(collection, hierarchy = [], options = {}) ⇒ Object
- #cullender_collection_or_select(collection, hierarchy = [], options = {}) ⇒ Object
- #extract_field_type(options) ⇒ Object
- #extract_triggers(params) ⇒ Object
- #filter_to_s(key, value) ⇒ Object
- #formatted_triggers(hash) ⇒ Object
- #link_to_filter(attribute, value, options = {}) ⇒ Object
- #link_to_remove_filter(attribute, value, options = {}) ⇒ Object
-
#nested_property_prefix(hierarchy, prefix = nil) ⇒ Object
nest is an array of values of the hierarchy.
-
#property_item(name, field, options, value_hash = {}, hierarchy = []) ⇒ Object
type = Event.property_types.has_key?(field) ? Event.property_types : “string” #Event::FIELDTYPE_MAP.
- #property_value_field(field, type, value = nil, options = {}, prefix = '') ⇒ Object
- #remove_filter_link(attribute, value, count, options = {}) ⇒ Object
- #render_property_tree(name, mapping, hash, options = {}, hierarchy = [], &block) ⇒ Object
Instance Method Details
#cullender_collection_and_select(collection, hierarchy = [], options = {}) ⇒ Object
74 75 76 77 78 79 80 |
# File 'app/helpers/cullender/rules_helper.rb', line 74 def cullender_collection_and_select(collection, hierarchy = [], = {}) prefix = nested_property_prefix(hierarchy) content_tag :div, :class => "add_filter" do select_tag( "and[field]#{prefix}", (collection), :class => "column_select_field") + submit_tag("AND", :name => "and[commit]#{prefix}", :class => 'btn') end end |
#cullender_collection_or_select(collection, hierarchy = [], options = {}) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'app/helpers/cullender/rules_helper.rb', line 64 def cullender_collection_or_select(collection, hierarchy = [], = {}) is_base = .delete(:base) prefix = nested_property_prefix(hierarchy) content_tag :div, :class => "add_group" do select_tag( "or[#{"raise_" if is_base}field]#{prefix}", (collection), :class => "column_select_field") + submit_tag("OR", :name => "or#{is_base ? "[raise]" : "[commit]"}#{prefix}", :class => 'btn') end end |
#extract_field_type(options) ⇒ Object
23 24 25 |
# File 'app/helpers/cullender/rules_helper.rb', line 23 def extract_field_type() .has_key?(:type) && Cullender::Rule::FIELD_TYPE_MAP.has_key?([:type]) ? Cullender::Rule::FIELD_TYPE_MAP[[:type]] : "text_field" end |
#extract_triggers(params) ⇒ Object
82 83 84 85 86 87 88 |
# File 'app/helpers/cullender/rules_helper.rb', line 82 def extract_triggers(params) if params.has_key?("triggers") params["triggers"] elsif params.has_key?("rule") && params["rule"].has_key?("triggers") params["rule"]["triggers"] end end |
#filter_to_s(key, value) ⇒ Object
108 109 110 |
# File 'app/helpers/cullender/rules_helper.rb', line 108 def filter_to_s(key, value) "#{key} #{Rule::FILTER_MAP[value["o"]]} #{value["v"]}" if value.has_key?("o") && value.has_key?("v") end |
#formatted_triggers(hash) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'app/helpers/cullender/rules_helper.rb', line 117 def formatted_triggers(hash) ands = [] ors = [] hash.each do |key, value| if value.keys.include?("o") ands << filter_to_s(key, value) else ors << formatted_triggers(value) end end if hash.present? ors.empty? ? "("+ ands.join(" AND ") + ")" : "(" + ors.join(" OR ") + ")" end |
#link_to_filter(attribute, value, options = {}) ⇒ Object
41 42 43 44 45 |
# File 'app/helpers/cullender/rules_helper.rb', line 41 def link_to_filter(attribute, value, = {}) copy_of_params = deep_clone(params) copy_of_params.deep_merge!({:filter => {attribute.to_sym => {"v" => value}}}) link_to_unless( params.has_key?(:filter) && params[:filter].has_key?(attribute), value, copy_of_params, ) end |
#link_to_remove_filter(attribute, value, options = {}) ⇒ Object
47 48 49 50 51 52 |
# File 'app/helpers/cullender/rules_helper.rb', line 47 def link_to_remove_filter(attribute, value, = {}) condition = params.has_key?(:filter) && params[:filter].has_key?(attribute.to_s) && params[:filter][attribute.to_s]['v'].to_s == value.to_s copy_of_params = deep_clone(params) copy_of_params[:filter].delete(attribute) link_to( text_with_close_btn("#{attribute}=#{value}"), copy_of_params, ) end |
#nested_property_prefix(hierarchy, prefix = nil) ⇒ Object
nest is an array of values of the hierarchy
113 114 115 |
# File 'app/helpers/cullender/rules_helper.rb', line 113 def nested_property_prefix(hierarchy, prefix = nil) "#{prefix}#{hierarchy.inject("") {|result, item| result += "[#{item}]"}}" end |
#property_item(name, field, options, value_hash = {}, hierarchy = []) ⇒ Object
type = Event.property_types.has_key?(field) ? Event.property_types : “string” #Event::FIELDTYPE_MAP
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'app/helpers/cullender/rules_helper.rb', line 28 def property_item(name, field, , value_hash = {}, hierarchy = []) fieldtype = extract_field_type() prefix = nested_property_prefix(hierarchy, name) operator = value_hash['o'] || nil value = value_hash['v'] || nil content_tag :div, :class => "item #{field.to_s.parameterize}" do submit_tag("-", :name => "remove#{nested_property_prefix(hierarchy, "")}[#{field}]", :class => 'btn') + label_tag(field, field, {:for => "#{prefix}[#{field}][v]"}) + select_tag( "#{prefix}[#{field}][o]", (Cullender::Rule::OPERATORS[fieldtype], operator), :class => "operator") + property_value_field(field, fieldtype, value, , prefix) end end |
#property_value_field(field, type, value = nil, options = {}, prefix = '') ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/helpers/cullender/rules_helper.rb', line 91 def property_value_field(field, type, value = nil, = {}, prefix = '') default = .delete(:default) case type when "number_field", "range_field", "text_field", "check_box" send("#{type}_tag".to_sym, "#{prefix}[#{field}][v]", value, {:class => "value"} ) when "select" select_tag("#{prefix}[#{field}][v]", ({}, value), {:class => "value"}) when "datetime" select_datetime set_datetime(value, default), {:class => "value", :prefix => "#{prefix}[#{field}][v]"} when "date" select_date set_date(value, default), {:class => "value", :prefix => "#{prefix}[#{field}][v]"} else #this includes Event::TEXT and any other field type text_field_tag "#{prefix}[#{field}][v]", value, {:class => "value"} end end |
#remove_filter_link(attribute, value, count, options = {}) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'app/helpers/cullender/rules_helper.rb', line 54 def remove_filter_link(attribute, value, count, = {}) copy_of_params = deep_clone(params) if params.has_key?(:filter) && params[:filter].has_key?(attribute.to_s) && params[:filter][attribute.to_s]['v'].to_s == value.to_s copy_of_params[:filter].delete(attribute.to_s) link_to "remove", copy_of_params, else content_tag(:span, count, ) end end |
#render_property_tree(name, mapping, hash, options = {}, hierarchy = [], &block) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'app/helpers/cullender/rules_helper.rb', line 5 def render_property_tree(name, mapping, hash, = {}, hierarchy = [], &block) .merge!(:class => "group level_#{hierarchy.length}") last_key = hash.keys.last html = content_tag :div, do hash.each do |field, value| if value.keys.include?("o") = mapping.has_key?(field) ? mapping[field] : {} block.call property_item(name, field, , value, hierarchy), last_key == field ? 1 : -1, hierarchy else hierarchy.push(field) block.call render_property_tree(name, mapping, value, {}, hierarchy, &block), last_key == field ? 0 : -1, hierarchy end end end if hash.present? hierarchy.pop html end |