Class: ActiveAdminAddons::TagBuilder

Inherits:
CustomBuilder show all
Defined in:
lib/activeadmin_addons/addons/tag_builder.rb

Instance Attribute Summary

Attributes inherited from CustomBuilder

#args, #block, #context, #model

Instance Method Summary collapse

Methods inherited from CustomBuilder

builder_method_name, create_view_methods, #initialize

Constructor Details

This class inherits a constructor from ActiveAdminAddons::CustomBuilder

Instance Method Details

#append_tagObject



14
15
16
17
18
# File 'lib/activeadmin_addons/addons/tag_builder.rb', line 14

def append_tag
  return context.status_tag(display_data, class: data) unless options[:interactive]

  select_tag
end

#display_dataObject



38
39
40
41
42
43
44
# File 'lib/activeadmin_addons/addons/tag_builder.rb', line 38

def display_data
  if @enum_attr == :enumerize
    data.text
  else
    EnumUtils.translate_enum_option(model.class, attribute.to_s, data)
  end
end

#enumerize_attr?Boolean

Returns:

  • (Boolean)


82
83
84
85
86
# File 'lib/activeadmin_addons/addons/tag_builder.rb', line 82

def enumerize_attr?
  data.is_a?("Enumerize::Value".constantize)
rescue NameError
  false
end

#interactive_params(klass) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/activeadmin_addons/addons/tag_builder.rb', line 46

def interactive_params(klass)
  {
    'class' => klass,
    'data-model' => class_name,
    'data-object_id' => model.id,
    'data-field' => attribute,
    'data-url' => resource_url,
    'data-value' => data
  }
end

#interactive_tag_paramsObject



57
58
59
60
61
62
63
# File 'lib/activeadmin_addons/addons/tag_builder.rb', line 57

def interactive_tag_params
  interactive_tag_class = 'interactive-tag'
  interactive_tag_class += " #{class_name}"
  interactive_tag_class += " #{class_name}-#{attribute}-#{model.id}-tag"
  interactive_tag_class += ' notify-success' if options[:success_message]
  interactive_params(interactive_tag_class)
end

#interactive_tag_select_paramsObject



65
66
67
68
69
70
71
# File 'lib/activeadmin_addons/addons/tag_builder.rb', line 65

def interactive_tag_select_params
  interactive_tag_select_klass = 'interactive-tag-select'
  interactive_tag_select_klass += " #{class_name}-#{attribute}-#{model.id}-select"
  interactive_tag_select_klass += ' select-container-hidden'
  interactive_tag_select_klass += ' default-select'
  interactive_params(interactive_tag_select_klass)
end

#possible_valuesObject



73
74
75
76
77
78
79
80
# File 'lib/activeadmin_addons/addons/tag_builder.rb', line 73

def possible_values
  klass = model.class
  if @enum_attr == :enumerize
    klass.enumerized_attributes[attribute.to_s].values.map { |value| [value.capitalize, value] }
  else
    EnumUtils.options_for_select(klass, attribute.to_s)
  end
end

#rails_enum_attr?Boolean

Returns:

  • (Boolean)


88
89
90
91
# File 'lib/activeadmin_addons/addons/tag_builder.rb', line 88

def rails_enum_attr?
  defined? Rails && Rails::VERSION::MAJOR == 4 && Rails::VERSION::MINOR >= 1 &&
    model.defined_enums[attribute.to_s]
end

#renderObject



3
4
5
6
7
8
9
10
11
12
# File 'lib/activeadmin_addons/addons/tag_builder.rb', line 3

def render
  @enum_attr = if enumerize_attr?
                 :enumerize
               elsif rails_enum_attr?
                 :enum
               end

  raise "you need to pass an enumerize or enum attribute" unless @enum_attr
  append_tag
end

#select_tagObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/activeadmin_addons/addons/tag_builder.rb', line 20

def select_tag
  context.div class: 'tag-select-container' do
    context.div(interactive_tag_params) do
      context.status_tag(display_data, class: data)
    end

    context.div(interactive_tag_select_params) do
      context.select do
        possible_values.each do |label, value|
          context.option(value: value, selected: value == data) do
            context.text_node label
          end
        end
      end
    end
  end
end