Module: ActiveAdminSimpleLife::SimpleElements

Includes:
Extensions
Defined in:
lib/active_admin_simple_life/simple_elements.rb

Instance Method Summary collapse

Instance Method Details

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

it check only for gender field for now



41
42
43
44
45
46
47
48
49
# File 'lib/active_admin_simple_life/simple_elements.rb', line 41

def filter_for_main_fields(klass, options ={})
  klass.main_fields.each do |f|
    if f == :gender
      filter ExtensionedSymbol.new(f).cut_id, collection: genders
    else
      filter ExtensionedSymbol.new(f).cut_id
    end
  end
end

#form_for_main_fields(klass, options = {}, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/active_admin_simple_life/simple_elements.rb', line 51

def form_for_main_fields(klass, options={}, &block)
  form do |f|
    f.semantic_errors(*f.object.errors.keys)
    f.inputs do
      klass.main_fields.each do |ff|
        ff_cut_id = ExtensionedSymbol.new(ff).cut_id
        current_options = options[ff] || options[ff_cut_id] || {}
        if ff == ff_cut_id
          f.input ff_cut_id
        else
          f.input ff_cut_id, {as: :select, member_label: :to_s}.merge(current_options)
        end
      end
      f.instance_eval(&block) if block_given?
    end
    f.actions
  end
end

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



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_admin_simple_life/simple_elements.rb', line 6

def index_for_main_fields(klass, options = {})
  max_length = options[:max_length]
  add_fields = [options[:add]].flatten.compact
  position = options[:position]
  index download_links: false do
    selectable_column
    id_column
    klass.main_fields.insert(position, *add_fields).each do |symbol|
      column(I18n.t("activerecord.attributes.#{klass.to_s.underscore}.#{symbol}"), sortable: symbol) do |current|
        plural_symbol = symbol.to_s.pluralize  
        field_value = current.send(ExtensionedSymbol.new(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
          if klass.respond_to?(plural_symbol)
            field_value && I18n.t("activerecord.attributes.#{klass.underscore}.#{plural_symbol}.#{field_value}") 
          else
            truncate_field field_value.to_s, max_length
          end
        end
      end
    end
    actions
  end
end

#nested_form_for_main_fields(klass, nested_klass, options = {}) ⇒ Object

simple nested set for 2 classes with defined main_fields



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/active_admin_simple_life/simple_elements.rb', line 71

def nested_form_for_main_fields(klass, nested_klass, options={})
  form_for_main_fields(klass,options) 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 { |f| ExtensionedSymbol.new(f).cut_id }.each do |nested_field|
        current_options = options.fetch(nested_table_name){{}}.fetch(nested_field){{}}
        form.input(nested_field, current_options) unless nested_field == main_model_name
      end
    end
  end
end