Module: ActiveAdminSimpleLife::SimpleElements

Defined in:
lib/active_admin_simple_life/menu_elements.rb

Instance Method Summary collapse

Instance Method Details

#cut_idObject



59
60
61
# File 'lib/active_admin_simple_life/menu_elements.rb', line 59

def cut_id
  to_s.sub(/_id$/, "").to_sym
end

#filter_for_main_fields(klass) ⇒ Object



31
32
33
# File 'lib/active_admin_simple_life/menu_elements.rb', line 31

def filter_for_main_fields(klass)
  klass.main_fields.each { |f| filter f.cut_id }
end

#form_for_main_fields(klass, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/active_admin_simple_life/menu_elements.rb', line 35

def form_for_main_fields(klass, &block)
  form do |f|
    f.semantic_errors(*f.object.errors.keys)
    f.inputs do
      klass.main_fields.each { |ff| f.input ff.cut_id }
      f.instance_eval(&block) if block_given?
    end
    f.actions
  end
end

#index_for_main_fields(klass, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/active_admin_simple_life/menu_elements.rb', line 4

def index_for_main_fields(klass, options = {})
  max_length = options[:max_length]
  index download_links: false do
    selectable_column
    id_column
    klass.main_fields.each do |symbol|
      column(I18n.t("activerecord.attributes.#{klass.to_s.underscore}.#{symbol}"), sortable: symbol) do |current|
        field_value = current.send(symbol.cut_id)
        case field_value
        when ActiveRecord::Base
          link_to truncate_field(field_value, max_length),
                  send(fetch_path(field_value), field_value.id)
        when ::ActiveSupport::TimeWithZone, Time, Date
          I18n.l field_value, format: :long
        when TrueClass
          span_true
        when FalseClass
          span_false
        else
          truncate_field field_value.to_s, max_length
        end
      end
    end
    actions
  end
end

#nested_form_for_main_fields(klass, nested_klass) ⇒ Object

simple nested set for 2 classes with defined main_fields



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/active_admin_simple_life/menu_elements.rb', line 47

def nested_form_for_main_fields(klass, nested_klass)
  form_for_main_fields(klass) do |form_field|
    nested_table_name = nested_klass.to_s.underscore.pluralize.to_sym
    main_model_name = klass.to_s.underscore.to_sym
    form_field.has_many nested_table_name, allow_destroy: true do |form|
      nested_klass.main_fields.map(&:cut_id).each do |nested_field|
        form.input(nested_field) unless nested_field == main_model_name
      end
    end
  end
end