Module: AzaharaSchema::ApplicationHelper

Defined in:
app/helpers/azahara_schema/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#attribute_formatter_for(schema_or_model, **options) ⇒ Object



81
82
83
# File 'app/helpers/azahara_schema/application_helper.rb', line 81

def attribute_formatter_for(schema_or_model, **options)
  ::AzaharaSchema::Presenter.formatter_for(schema_or_model).new(schema_or_model, self, **options)
end

#azahara_attribute_for(model, name) ⇒ Object



77
78
79
# File 'app/helpers/azahara_schema/application_helper.rb', line 77

def azahara_attribute_for(model, name)
  ::AzaharaSchema::Schema.schema_class_for(model).attribute(model, name)
end

#azahara_export_path(schema, export_format) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'app/helpers/azahara_schema/application_helper.rb', line 55

def azahara_export_path(schema, export_format)
  model = if schema.model.is_a?(ActiveRecord::Relation)
    schema.model.klass
  else
    schema.model
  end

  polymorphic_path(model, schema.to_param.merge(format: export_format))
rescue NoMethodError => err
  nil
end

#azahara_filter_row(schema, filter, name = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/helpers/azahara_schema/application_helper.rb', line 38

def azahara_filter_row(schema, filter, name=nil)
  name ||= filter.filter_name
  (:div, class: 'form-group row filter', data: {name: name}) do
    s = ''.html_safe
    s << (:div, label_tag("f[#{name}]", filter.attribute_name.human), class: 'col-md-2 control-label')

    operators_tag = azahara_operators_tag(schema, filter, name)
    if schema.operators_for(name).count > 1
      s << (:div, operators_tag, class: 'col-md-2')
      s << (:div, filter_field(schema, filter), class: 'col-md-8')
    else
      s << (:div, operators_tag + filter_field(schema, filter), class: 'col-md-10')
    end
    s
  end
end

#azahara_operators_tag(schema, filter, name) ⇒ Object



20
21
22
23
24
25
26
27
# File 'app/helpers/azahara_schema/application_helper.rb', line 20

def azahara_operators_tag(schema, filter, name)
  field_name = "f[#{name}]"
  if schema.operators_for(name).count > 1
    select_tag field_name, options_for_select(operators_for_select(schema, name), schema.operator_for(name)), class: 'form-control operator-field'
  else
    hidden_field_tag field_name, schema.operators_for(name).first, class: 'operator-field'
  end
end

#filter_field(schema, filter) ⇒ Object



29
30
31
32
33
34
35
36
# File 'app/helpers/azahara_schema/application_helper.rb', line 29

def filter_field(schema, filter)
  case filter.format.format_name
  when 'list', 'love'
    select :f, filter.filter_name, options_for_select( list_values_for_select(filter), schema.value_for(filter.filter_name) ), {include_blank: true}, class: 'form-control value-field'
  else
    text_field :f, filter.filter_name, value: schema.value_for(filter.filter_name), class: 'form-control value-field'
  end
end

#label_record_action(model) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'app/helpers/azahara_schema/application_helper.rb', line 4

def label_record_action(model)
  model_name = model.model_name.human
  key = model.persisted? ? :edit : :new

  defaults = []
  defaults << :"helpers.action.#{model.model_name.i18n_key}.#{key}"
  defaults << :"helpers.action.#{key}"
  defaults << "#{key.to_s.humanize} #{model_name}"

  t(defaults.shift, model: model_name, default: defaults)
end

#list_values_for_select(attribute) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'app/helpers/azahara_schema/application_helper.rb', line 67

def list_values_for_select(attribute)
  if attribute.format.format_name == 'list'
    attribute.available_values.collect do |l, val|
      [attribute.attribute_name.human_list_value(l), val]
    end
  else
    attribute.available_values
  end
end

#operators_for_select(schema, filter_name) ⇒ Object



16
17
18
# File 'app/helpers/azahara_schema/application_helper.rb', line 16

def operators_for_select(schema, filter_name)
  schema.operators_for(filter_name).collect{|o| [o, o]}
end

#unfilled_attribute_messageObject



85
86
87
# File 'app/helpers/azahara_schema/application_helper.rb', line 85

def unfilled_attribute_message
  (:span, t('label_unfilled'), class: 'unfilled-message')
end