Sha256: a04fdb25794c3064a86f52d6b298beb81a5f424cc9f67ef4e1151c9a648ec94b

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

module VisualConditionBuilder
  module ApplicationHelper

    def build_conditions(dictionary, *args)
      dictionary_name = get_dictionary_name(dictionary)
      dictionary_klass = get_dictionary_klass(dictionary)
      container_name = "#{dictionary_name}_condition_container"

      hArgs = (args ||= []).reduce(Hash.new, :merge)
      hArgs = normalize_placeholder_label(hArgs)

      builder_options = {
          dictionary: dictionary_klass.dictionary(get_dictionary_context(dictionary), self.request)
      }.deep_merge(hArgs)

      render partial: 'visual_condition_builder/builder_conditions', locals: {container_name: container_name, builder_options: builder_options.to_json.html_safe}
    end

    def conditions_fields(dictionary)
      dictionary_name = get_dictionary_name(dictionary)
      dictionary_klass = get_dictionary_klass(dictionary)
      @container_name = "##{dictionary_name}_condition_container"
      @fields = dictionary_klass.fields(get_dictionary_context(dictionary))
      render partial: 'visual_condition_builder/conditions_fields'
    end

    private
    def get_dictionary_context(dictionary)
      dictionary.is_a?(Hash) ? dictionary.values.first : :default
    end
    def get_dictionary_name(dictionary)
      "#{dictionary.is_a?(Hash) ? dictionary.keys.first : dictionary}_#{get_dictionary_context(dictionary)}"
    end
    def get_dictionary_klass(dictionary)
      "#{dictionary.is_a?(Hash) ? dictionary.keys.first : dictionary}_dictionary".classify.constantize
    end

    def normalize_placeholder_label(args)
      args[:placeholder] ||= {}
      [:fields, :operators, :values].each do |attr|
        unless args[:placeholder][attr].present?
          label = I18n.t(attr, default: [''], scope: [:condition_builder, :placeholder])
          args[:placeholder][attr] = label if label.present?
        end
      end
      args
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
visual_condition_builder-2.1.2 app/helpers/visual_condition_builder/application_helper.rb