Class: LcpRuby::Dsl::FormBuilder

Inherits:
Object
  • Object
show all
Includes:
SourceLocationCapture
Defined in:
lib/lcp_ruby/dsl/presenter_builder.rb

Instance Method Summary collapse

Methods included from SourceLocationCapture

capture_source_loc

Constructor Details

#initializeFormBuilder

Returns a new instance of FormBuilder.



963
964
965
966
967
968
969
# File 'lib/lcp_ruby/dsl/presenter_builder.rb', line 963

def initialize
  @sections = []
  @layout_value = nil
  @includes_list = nil
  @eager_load_list = nil
  @description_value = nil
end

Instance Method Details

#custom_section(title = nil, partial:, key: nil, visible_when: nil, additional_permitted_params: nil) ⇒ Object

Raises:

  • (ArgumentError)


980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
# File 'lib/lcp_ruby/dsl/presenter_builder.rb', line 980

def custom_section(title = nil, partial:, key: nil, visible_when: nil, additional_permitted_params: nil)
  raise ArgumentError, "custom_section requires either a title or key:" if title.nil? && key.nil?
  section_hash = {
    "type" => "custom",
    "partial" => partial.to_s
  }
  section_hash["title"] = title if title
  section_hash["section_key"] = key.to_s if key
  section_hash["visible_when"] = stringify_deep(visible_when) if visible_when
  if additional_permitted_params
    section_hash["additional_permitted_params"] = additional_permitted_params.map do |p|
      p.is_a?(Hash) ? stringify_deep(p) : p.to_s
    end
  end
  @sections << section_hash
end

#description(text) ⇒ Object



971
972
973
974
# File 'lib/lcp_ruby/dsl/presenter_builder.rb', line 971

def description(text)
  @description_value = text
  @description_source_loc = capture_source_loc
end

#eager_load(*assocs) ⇒ Object



1064
1065
1066
# File 'lib/lcp_ruby/dsl/presenter_builder.rb', line 1064

def eager_load(*assocs)
  @eager_load_list = assocs.flatten.map { |a| a.is_a?(Hash) ? HashUtils.stringify_deep(a) : a.to_s }
end

#includes(*assocs) ⇒ Object



1060
1061
1062
# File 'lib/lcp_ruby/dsl/presenter_builder.rb', line 1060

def includes(*assocs)
  @includes_list = assocs.flatten.map { |a| a.is_a?(Hash) ? HashUtils.stringify_deep(a) : a.to_s }
end

#layout(value) ⇒ Object



976
977
978
# File 'lib/lcp_ruby/dsl/presenter_builder.rb', line 976

def layout(value)
  @layout_value = value.to_s
end

#nested_fields(title = nil, association: nil, json_field: nil, target_model: nil, key: nil, description: nil, allow_add: true, allow_remove: true, min: nil, max: nil, add_label: nil, empty_message: nil, sortable: false, columns: nil, visible_when: nil, disable_when: nil, &block) ⇒ Object



1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
# File 'lib/lcp_ruby/dsl/presenter_builder.rb', line 1018

def nested_fields(title = nil, association: nil, json_field: nil, target_model: nil,
                  key: nil, description: nil, allow_add: true, allow_remove: true,
                  min: nil, max: nil, add_label: nil, empty_message: nil,
                  sortable: false, columns: nil, visible_when: nil, disable_when: nil, &block)
  if association && json_field
    raise ArgumentError, "nested_fields cannot have both association: and json_field:"
  end
  unless association || json_field
    raise ArgumentError, "nested_fields requires either association: or json_field:"
  end

  section_hash = {
    "type" => "nested_fields",
    "allow_add" => allow_add,
    "allow_remove" => allow_remove
  }
  section_hash["title"] = title if title
  section_hash["section_key"] = key.to_s if key
  section_hash["association"] = association.to_s if association
  section_hash["json_field"] = json_field.to_s if json_field
  section_hash["target_model"] = target_model.to_s if target_model
  section_hash["description"] = description if description
  section_hash["columns"] = columns if columns
  section_hash["min"] = min if min
  section_hash["max"] = max if max
  section_hash["add_label"] = add_label if add_label
  section_hash["empty_message"] = empty_message if empty_message
  section_hash["sortable"] = sortable if sortable
  section_hash["visible_when"] = stringify_deep(visible_when) if visible_when
  section_hash["disable_when"] = stringify_deep(disable_when) if disable_when
  if block
    builder = NestedSectionBuilder.new
    builder.instance_eval(&block)
    if builder.has_sub_sections?
      section_hash["sub_sections"] = builder.to_sub_sections
    else
      section_hash["fields"] = builder.to_fields
    end
  end
  @sections << section_hash
end

#section(title = nil, key: nil, columns: 1, description: nil, responsive: nil, collapsible: false, collapsed: false, visible_when: nil, disable_when: nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
# File 'lib/lcp_ruby/dsl/presenter_builder.rb', line 997

def section(title = nil, key: nil, columns: 1, description: nil, responsive: nil, collapsible: false, collapsed: false,
            visible_when: nil, disable_when: nil, &block)
  raise ArgumentError, "section requires either a title or key:" if title.nil? && key.nil?
  section_hash = { "columns" => columns }
  section_hash["title"] = title if title
  section_hash["_label_source_loc"] = capture_source_loc if title
  section_hash["section_key"] = key.to_s if key
  section_hash["description"] = description if description
  section_hash["responsive"] = stringify_deep(responsive) if responsive
  section_hash["collapsible"] = collapsible if collapsible
  section_hash["collapsed"] = collapsed if collapsed
  section_hash["visible_when"] = stringify_deep(visible_when) if visible_when
  section_hash["disable_when"] = stringify_deep(disable_when) if disable_when
  if block
    builder = SectionBuilder.new
    builder.instance_eval(&block)
    section_hash["fields"] = builder.to_fields
  end
  @sections << section_hash
end

#to_hashObject



1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
# File 'lib/lcp_ruby/dsl/presenter_builder.rb', line 1068

def to_hash
  hash = {}
  hash["description"] = @description_value if @description_value
  hash["_description_source_loc"] = @description_source_loc if @description_source_loc
  hash["sections"] = @sections
  hash["layout"] = @layout_value if @layout_value
  hash["includes"] = @includes_list if @includes_list
  hash["eager_load"] = @eager_load_list if @eager_load_list
  hash
end