Class: RelatedSetField

Inherits:
Field
  • Object
show all
Defined in:
lib/generators/hot_glue/fields/related_set_field.rb

Instance Attribute Summary collapse

Attributes inherited from Field

#alt_lookup, #assoc_label, #assoc_model, #associations, #attachment_data, #auth, #class_name, #default_boolean_display, #display_as, #form_labels_position, #form_placeholder_labels, #god, #hawk_keys, #hidden_create, #hidden_update, #layout_strategy, #limit, #modify_as, #name, #namespace, #object, #ownership_field, #plural, #polymorphic_parents, #pundit, #sample_file_path, #self_auth, #singular, #singular_class, #sql_type, #stimmify, #update_show_only

Instance Method Summary collapse

Methods inherited from Field

#code_to_reset_match_if_search_is_blank, #display_boolean_as, #field_error_name, #field_output, #field_view_output, #form_show_only_output, #getName, #hidden_output, #label_class, #label_for, #line_field_output, #modified_display_output, #modify_binary?, #newline_after_field?, #prelookup_syntax, #spec_list_view_assertion, #spec_list_view_natural_assertion, #spec_make_assertion, #spec_random_data, #spec_related_column_lets, #spec_setup_and_change_act, #spec_setup_let_arg, #testing_name, #text_area_output

Constructor Details

#initialize(scaffold:, name:) ⇒ RelatedSetField

Returns a new instance of RelatedSetField.



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
30
31
32
33
34
35
36
37
# File 'lib/generators/hot_glue/fields/related_set_field.rb', line 5

def initialize( scaffold: , name: )
  super

  @related_set_model = eval("#{class_name}.reflect_on_association(:#{name})")

  if @related_set_model.nil?
        raise "You specified a related set #{name} but there is no association on #{singular_class} for #{name}; please add a `has_and_belongs_to_many :#{name}` OR a `has_many :#{name}, through: ...` to the #{singular_class} model"

    # exit_message = "*** Oops: The model #{class_name} is missing an association for :#{assoc_name} or the model #{assoc_name.titlecase} doesn't exist. TODO: Please implement a model for #{assoc_name.titlecase}; or add to #{class_name} `belongs_to :#{assoc_name}`.  To make a controller that can read all records, specify with --god."
    puts exit_message
    raise(HotGlue::Error, exit_message)
  end

  @assoc_class = eval(@related_set_model.try(:class_name))


  @deets = scaffold.related_sets[name.to_sym]

  if @deets[:label_field].nil?
    @label_field = "label"

    # name_list = [:name, :to_label, :full_name, :display_name, :email]
    #
    # if assoc_class && name_list.collect{ |field|
    #   assoc_class.respond_to?(field.to_s) || assoc_class.instance_methods.include?(field)
    # }.none?
    #   exit_message = "Oops: Missing a label for `#{assoc_class}`. Can't find any column to use as the display label for the #{@assoc_name} association on the #{class_name} model. TODO: Please implement just one of: 1) name, 2) to_label, 3) full_name, 4) display_name 5) email. You can implement any of these directly on your`#{assoc_class}` model (can be database fields or model methods) or alias them to field you want to use as your display label. Then RERUN THIS GENERATOR. (Field used will be chosen based on rank here.)"
    #   raise(HotGlue::Error, exit_message)
    # end
  else
    @label_field = @deets[:label_field]
  end
end

Instance Attribute Details

#assocObject

Returns the value of attribute assoc.



3
4
5
# File 'lib/generators/hot_glue/fields/related_set_field.rb', line 3

def assoc
  @assoc
end

#assoc_classObject

Returns the value of attribute assoc_class.



3
4
5
# File 'lib/generators/hot_glue/fields/related_set_field.rb', line 3

def assoc_class
  @assoc_class
end

#assoc_nameObject

Returns the value of attribute assoc_name.



3
4
5
# File 'lib/generators/hot_glue/fields/related_set_field.rb', line 3

def assoc_name
  @assoc_name
end

#hawkObject

Returns the value of attribute hawk.



3
4
5
# File 'lib/generators/hot_glue/fields/related_set_field.rb', line 3

def hawk
  @hawk
end

#label_fieldObject

Returns the value of attribute label_field.



3
4
5
# File 'lib/generators/hot_glue/fields/related_set_field.rb', line 3

def label_field
  @label_field
end

Instance Method Details

#association_class_nameObject



67
68
69
# File 'lib/generators/hot_glue/fields/related_set_field.rb', line 67

def association_class_name
  eval("#{class_name}.reflect_on_association(:#{name})").class_name
end

#association_ids_methodObject



63
64
65
# File 'lib/generators/hot_glue/fields/related_set_field.rb', line 63

def association_ids_method
  eval("#{class_name}.reflect_on_association(:#{name})").class_name.underscore + "_ids"
end

#form_field_outputObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/generators/hot_glue/fields/related_set_field.rb', line 40

def form_field_output
  disabled_syntax = +""

  if pundit
    disabled_syntax << ", {disabled: ! #{class_name}Policy.new(#{auth}, @#{singular}).role_ids_able?}"
  end
  " <%= f.collection_check_boxes :#{association_ids_method}, #{record_scope}, :id, :#{label_field}, {}#{disabled_syntax} do |m| %>
  <%= m.check_box %> <%= m.label %><br />
<% end %>"
end

#record_scopeObject



51
52
53
54
55
56
57
# File 'lib/generators/hot_glue/fields/related_set_field.rb', line 51

def record_scope
  if hawk.nil?
    "#{association_class_name}.all"
  else
    hawk
  end
end

#viewable_outputObject



71
72
73
# File 'lib/generators/hot_glue/fields/related_set_field.rb', line 71

def viewable_output
  "<%= #{singular}.#{name}.collect(&:#{label_field}).join(\", \") %>"
end