Class: Infold::FieldGroup

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/infold/field_group.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table = nil) ⇒ FieldGroup

Returns a new instance of FieldGroup.



10
11
12
# File 'lib/infold/field_group.rb', line 10

def initialize(table=nil)
  init_fields(table)
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



7
8
9
# File 'lib/infold/field_group.rb', line 7

def fields
  @fields
end

#has_association_model=(value) ⇒ Object (writeonly)

Sets the attribute has_association_model

Parameters:

  • value

    the value to set the attribute has_association_model to.



8
9
10
# File 'lib/infold/field_group.rb', line 8

def has_association_model=(value)
  @has_association_model = value
end

Instance Method Details

#active_storage_fieldsObject



39
40
41
# File 'lib/infold/field_group.rb', line 39

def active_storage_fields
  select { |f| f.active_storage.present? }
end

#add_field(name, type = nil) ⇒ Object



23
24
25
# File 'lib/infold/field_group.rb', line 23

def add_field(name, type=nil)
  (@fields << Field.new(name, type&.to_sym)).last
end

#association_fieldsObject



31
32
33
# File 'lib/infold/field_group.rb', line 31

def association_fields
  select { |f| f.association.present? }
end

#association_search_list_fieldsObject



106
107
108
# File 'lib/infold/field_group.rb', line 106

def association_search_list_fields
  select { |f| f.in_association_search_list? }.sort_by { |f| f.association_search_list_seq }
end

#associationsObject



35
36
37
# File 'lib/infold/field_group.rb', line 35

def associations
  association_fields.map(&:association)
end

#condition_fields(kind = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/infold/field_group.rb', line 58

def condition_fields(kind=nil)
  fields =
    case kind.to_s
    when 'index'
      select { |f| f.search_conditions.find(&:in_index?).present? }
    when 'association_search'
      select { |f| f.search_conditions.find(&:in_association_search?).present? }
    else
      select { |f| f.search_conditions.present? }
    end
  if fields.blank?
    # 条件が未設定の場合、idを対象とする
    id_field = find_or_initialize_field(:id)
    id_field.add_search_condition(kind || :index,
                                  sign: 'eq',
                                  form_kind: :text)
    fields = [id_field]
  end
  fields
end

#conditions(kind = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/infold/field_group.rb', line 79

def conditions(kind=nil)
  conditions = condition_fields(kind).map(&:search_conditions).flatten
  if kind.to_s == 'index'
    conditions.sort_by(&:index_seq)
  elsif kind.to_s == 'association_search'
    conditions.sort_by(&:association_seq)
  else
    conditions
  end
end

#csv_fieldsObject



98
99
100
# File 'lib/infold/field_group.rb', line 98

def csv_fields
  select { |f| f.in_csv? }.sort_by { |f| f.csv_seq }
end

#datetime_fieldsObject



27
28
29
# File 'lib/infold/field_group.rb', line 27

def datetime_fields
  select{ |f| f.type.to_s == 'datetime' && !%w(created_at updated_at).include?(f.name) }
end

#decorator_fieldsObject



51
52
53
54
55
56
# File 'lib/infold/field_group.rb', line 51

def decorator_fields
  decorator_fields = select { |f| f.decorator.present? }.to_a
  decorator_fields + select { |field| %i(datetime date boolean).include?(field.type) }.each do |field|
    field.build_decorator(kind: field.type.to_sym)
  end.to_a
end

#eachObject



14
15
16
# File 'lib/infold/field_group.rb', line 14

def each
  @fields.each { |field| yield field }
end

#enum_fieldsObject



47
48
49
# File 'lib/infold/field_group.rb', line 47

def enum_fields
  select { |f| f.enum.present? }
end

#find_or_initialize_field(field_name) ⇒ Object



18
19
20
21
# File 'lib/infold/field_group.rb', line 18

def find_or_initialize_field(field_name)
  find { |field| field.association&.name == field_name || field.name == field_name } ||
    (@fields << Field.new(field_name)).last
end

#form_fieldsObject



90
91
92
# File 'lib/infold/field_group.rb', line 90

def form_fields
  select { |f| f.form_element.present? }.sort_by { |f| f.form_element.seq }
end

#has_association_model?Boolean

Returns:

  • (Boolean)


110
# File 'lib/infold/field_group.rb', line 110

def has_association_model?; @has_association_model end

#index_list_fieldsObject



94
95
96
# File 'lib/infold/field_group.rb', line 94

def index_list_fields
  select { |f| f.in_index_list? }.sort_by { |f| f.index_list_seq }
end

#show_fieldsObject



102
103
104
# File 'lib/infold/field_group.rb', line 102

def show_fields
  select { |f| f.show_element.present? }.sort_by { |f| f.show_element.seq }
end

#validation_fieldsObject



43
44
45
# File 'lib/infold/field_group.rb', line 43

def validation_fields
  select { |f| f.validation.present? }
end