Class: AssociationField

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

Direct Known Subclasses

UUIDField

Instance Attribute Summary collapse

Attributes inherited from Field

#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, #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_output, #field_view_output, #getName, #hidden_output, #label_class, #label_for, #modified_display_output, #modify_binary?, #spec_list_view_natural_assertion, #spec_random_data, #testing_name, #text_area_output, #viewable_output

Constructor Details

#initialize(scaffold:, name:) ⇒ AssociationField

Returns a new instance of AssociationField.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/generators/hot_glue/fields/association_field.rb', line 8

def initialize(scaffold: , name: )
  super
  @assoc_model = eval("#{class_name}.reflect_on_association(:#{assoc})")

  if assoc_model.nil?
    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

  if ! eval("#{class_name}.reflect_on_association(:#{assoc}).polymorphic?")
    @assoc_class = eval(assoc_model.try(:class_name))

    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
  end
end

Instance Attribute Details

#alt_lookupObject

Returns the value of attribute alt_lookup.



6
7
8
# File 'lib/generators/hot_glue/fields/association_field.rb', line 6

def alt_lookup
  @alt_lookup
end

#assocObject

Returns the value of attribute assoc.



6
7
8
# File 'lib/generators/hot_glue/fields/association_field.rb', line 6

def assoc
  @assoc
end

#assoc_classObject

Returns the value of attribute assoc_class.



6
7
8
# File 'lib/generators/hot_glue/fields/association_field.rb', line 6

def assoc_class
  @assoc_class
end

#assoc_nameObject

Returns the value of attribute assoc_name.



6
7
8
# File 'lib/generators/hot_glue/fields/association_field.rb', line 6

def assoc_name
  @assoc_name
end

#polymorphic_parentsObject

Returns the value of attribute polymorphic_parents.



6
7
8
# File 'lib/generators/hot_glue/fields/association_field.rb', line 6

def polymorphic_parents
  @polymorphic_parents
end

Instance Method Details

#assoc_labelObject



32
33
34
# File 'lib/generators/hot_glue/fields/association_field.rb', line 32

def assoc_label

end

#field_error_nameObject



171
172
173
# File 'lib/generators/hot_glue/fields/association_field.rb', line 171

def field_error_name
  assoc_name
end

#form_field_outputObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/generators/hot_glue/fields/association_field.rb', line 88

def form_field_output
  assoc_name = name.to_s.gsub("_id","")


  assoc = eval("#{class_name}.reflect_on_association(:#{assoc_name})")

  if alt_lookup.keys.include?(name)
    assoc_name = name.to_s.gsub("_id","")
    assoc = eval("#{class_name}.reflect_on_association(:#{assoc_name})")

    lookup_field = alt_lookup[name][:lookup_as]
    assoc = alt_lookup[name][:assoc].downcase
    parts = name.split('_')
    "<%= f.text_field :__lookup_#{assoc}_#{lookup_field}, value: @#{singular}.#{assoc_name}&.#{lookup_field}, placeholder: \"#{lookup_field}\" " + (stimmify ? ", 'data-#{@stimmify}-target': '#{camelcase_name}' " : "")  + "%>"
  elsif modify_as && modify_as[:typeahead]

    search_url  = "#{namespace ? namespace + "_" : ""}" +
      modify_as[:nested].join("_") + ( modify_as[:nested].any? ? "_" : "") +
      + "#{assoc.name.to_s.pluralize}_typeahead_index_url"

    if @modify_as[:nested].any?
      search_url  << "(" + modify_as[:nested].collect{|x| "#{x}"}.join(",") + ")"
    end
@polymorphic_parents  = []
    "<div class='typeahead typeahead--#{assoc.name}_id'
    data-controller='typeahead'
    data-typeahead-url-value='<%= #{search_url} %>'
    data-typeahead-typeahead-results-outlet='#search-results'>
    <%= text_field_tag :#{assoc.plural_name}_query, '', placeholder: 'Search #{assoc.plural_name}', class: 'search__input',
                   data: { action: 'keyup->typeahead#fetchResults keydown->typeahead#navigateResults', typeahead_target: 'query' },
                   autofocus: true,
                   autocomplete: 'off',
                   value: #{singular}.try(:#{assoc.name}).try(:name) %>
    <%= f.hidden_field :#{assoc.name}_id, value: #{singular}.try(:#{assoc.name}).try(:id), 'data-typeahead-target': 'hiddenFormValue' %>" + 
    ( @polymorphic_parents.include?( (assoc.name.to_s + "_id")) ? 
"\n     <%= f.hidden_field :#{assoc.name}_type, value: #{singular}.try(:#{assoc.name}_type), 'data-typeahead-target': 'hiddenFormType' %>"
    : "") + 
    "\n      <div data-typeahead-target='results'></div>
    <div data-typeahead-target='classIdentifier' data-id=\"typeahead--#{assoc_name}_id\"></div>
    </div>"
  else
    if assoc.nil?
      exit_message = "*** Oops. on the #{class_name} object, there doesn't seem to be an association called '#{assoc_name}'"
      exit
    end

    is_owner = name == ownership_field
    assoc_class_name = assoc.class_name.to_s
    if assoc_class
      display_column = HotGlue.derrive_reference_name(assoc_class_name)
    else
      # polymorphic
      display_column = "name"
    end

    if hawk_keys[assoc.foreign_key.to_sym]
      hawk_definition = hawk_keys[assoc.foreign_key.to_sym]
      hawked_association = hawk_definition[:bind_to].join(".")
    else
      hawked_association = "#{assoc.class_name}.all"
    end


    if @stimmify
      col_target = HotGlue.to_camel_case(name.to_s.gsub("_", " "))
      data_attr = ", data: {'#{@stimmify}-target': '#{col_target}'} "
    else
      data_attr = ""
    end



    if  modify_as && modify_as[:include_blank]
      include_blank = ", include_blank: true"
    end

    (is_owner ? "<% unless @#{assoc_name} %>\n" : "") +
      "  <%= f.collection_select(:#{name}, #{hawked_association}, :id, :#{display_column}, { prompt: true, selected: #{singular}.#{name}#{include_blank} }, class: 'form-control'#{data_attr}) %>\n" +
      (is_owner ? "<% else %>\n <%= @#{assoc_name}.#{display_column} %>" : "") +
      (is_owner ? "\n<% end %>" : "")
  end
end

#form_show_only_outputObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/generators/hot_glue/fields/association_field.rb', line 175

def form_show_only_output


  # if hawk_keys[assoc.foreign_key.to_sym]
  #   hawk_definition = hawk_keys[assoc.foreign_key.to_sym]
  #   hawked_association = hawk_definition.join(".")
  # else
  #   hawked_association = "#{assoc.class_name}.all"
  # end
  if modify_as && modify_as[:none]
    "<span class='badge #{modify_as[:badges]}'>" + raw_view_field + "</span>"
  else
    raw_view_field
  end
end

#line_field_outputObject



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/generators/hot_glue/fields/association_field.rb', line 191

def line_field_output


  if assoc_class
    display_column =  HotGlue.derrive_reference_name(assoc_class.to_s)
    "<%= #{singular}.#{assoc}.try(:#{display_column}) || '<span class=\"content \">MISSING</span>'.html_safe %>"
  else
    "<%= #{singular}.#{assoc}.try(:to_label) || '<span class=\"content \">MISSING</span>'.html_safe %>"
  end

end

#load_all_query_statementObject



275
276
277
278
279
280
281
# File 'lib/generators/hot_glue/fields/association_field.rb', line 275

def load_all_query_statement
  if modify_as && modify_as[:typeahead]
    "#{name}_query = association_constructor(:#{name}, @q['0'][:#{name}])"
  else
    "#{name}_query = association_constructor(:#{name}, @q['0'][:#{name}_search])"
  end
end

#newline_after_field?Boolean

Returns:

  • (Boolean)


263
264
265
266
267
268
269
# File 'lib/generators/hot_glue/fields/association_field.rb', line 263

def newline_after_field?
  if modify_as && modify_as[:typeahead]
    false
  else
    true
  end
end

#prelookup_syntaxObject



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/generators/hot_glue/fields/association_field.rb', line 283

def prelookup_syntax
  field =  @alt_lookup[name.to_s]
  if field[:with_create]

    method_name = "find_or_create_by"

  else
    method_name = "find_by"
  end
  field_name = field[:assoc].downcase.gsub("_id","")
  assoc_class = field[:assoc].classify

  assoc = plural

  ## TODO: add the hawk here
  res = +""
  if @hawk_keys[name.to_sym]
    res << "#{field_name} = #{@hawk_keys[name.to_sym][:bind_to].first}.#{method_name}(#{field[:lookup_as]}: #{singular}_params[:__lookup_#{field[:assoc].downcase}_#{field[:lookup_as]}] )"
  elsif @god
    assoc_name = field[:assoc]
    res << "#{field_name} = #{assoc_class}.#{method_name}(#{field[:lookup_as]}: #{singular}_params[:__lookup_#{field[:assoc].downcase}_#{field[:lookup_as]}] )"
  else
    raise "Field #{field_name} is an alt lookup in a non-Gd context which is a security vulnerability"
  end

  res << "\n    modified_params.tap { |hs| hs.delete(:__lookup_#{field[:assoc].downcase}_#{field[:lookup_as]})}"
  return res

end

#raw_view_fieldObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/generators/hot_glue/fields/association_field.rb', line 73

def raw_view_field
  assoc_name = name.to_s.gsub("_id","")
  assoc = eval("#{class_name}.reflect_on_association(:#{assoc_name})")
  if assoc.nil?
    exit_message = "*** Oops. on the #{singular_class} object, there doesn't seem to be an association called '#{assoc_name}'"
    exit
  end
  is_owner = name == ownership_field
  assoc_class_name = assoc.class_name.to_s
  display_column = HotGlue.derrive_reference_name(assoc_class_name)


  "<%= #{singular}.#{assoc_name}&.#{display_column} %>"
end

#search_field_outputObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/generators/hot_glue/fields/association_field.rb', line 204

def search_field_output

  assoc_name = name.to_s.gsub("_id","")
  assoc = eval("#{class_name}.reflect_on_association(:#{assoc_name})")
  if modify_as && modify_as[:typeahead]


    search_url  = "#{namespace ? namespace + "_" : ""}#{ modify_as[:nested][0] ? modify_as[:nested][0] + "_" : ""}#{assoc.name.to_s.pluralize}_typeahead_index_url"


    # \"q[0][#{name}_search]\"
    # @q['0']['#{name}_search']
    "<div class='typeahead typeahead--q_0_#{name}_search'
    data-controller='typeahead'
    data-typeahead-url-value='<%= #{search_url} %>'
    data-typeahead-typeahead-results-outlet='#search-results'>
    <%= text_field_tag \'q[0][#{name}_search]_query\', '', placeholder: 'Search #{assoc.plural_name}', class: 'search__input',
                   data: { action: 'keyup->typeahead#fetchResults keydown->typeahead#navigateResults', typeahead_target: 'query' },
                   autofocus: true,
                   autocomplete: 'off',
                   value: @q['0']['#{name}'].present? ? #{assoc.class_name}.find(@q['0']['#{name}']).try(:name) : \"\"  %>
    <%= f.hidden_field \'q[0][#{name}]\', value: @q['0']['#{name}'], 'data-typeahead-target': 'hiddenFormValue' %>
    <div data-typeahead-target='results'></div>
    <div data-typeahead-target='classIdentifier' data-id=\"typeahead--q_0_#{name}_search\"></div>
    </div>"
  else
    if assoc.nil?
      exit_message = "*** Oops. on the #{class_name} object, there doesn't seem to be an association called '#{assoc_name}'"
      exit
    end

    is_owner = name == ownership_field
    assoc_class_name = assoc.class_name.to_s
    display_column = HotGlue.derrive_reference_name(assoc_class_name)

    if hawk_keys[assoc.foreign_key.to_sym]
      hawk_definition = hawk_keys[assoc.foreign_key.to_sym]
      hawked_association = hawk_definition[:bind_to].join(".")
    else
      hawked_association = "#{assoc.class_name}.all"
    end

    (is_owner ? "<% unless @#{assoc_name} %>\n" : "") +
      "  <%= f.collection_select(\"q[0][#{name}_search]\", #{hawked_association}, :id, :#{display_column}, {include_blank: true, selected: @q['0']['#{name}_search'] }, class: 'form-control') %>\n" +
      (is_owner ? "<% else %>\n <%= @#{assoc_name}.#{display_column} %>" : "") +
      (is_owner ? "\n<% end %>" : "")
  end


  # "      "+
  #   "\n        <%= f.select 'q[0][#{name}_match]', options_for_select([['', ''], ['is on', 'is_on'], " +
  #   "\n         ['is between', 'is_between'], ['is on or after', 'is_on_or_after'], " +
  #   "\n         ['is before or on', 'is_before_or_on'], ['not on', 'not_on']], @q[\'0\']['#{name}_match'] ), {} ," +
  #   "\n         { class: 'form-control match', 'data-action': 'change->date-range-picker#matchSelection' } %>"+
  #   "\n        <%= date_field_localized f, 'q[0][#{name}_search_start]',  @q[\'0\'][:#{name}_search_start], autocomplete: 'off', size: 40, class: 'form-control', type: 'text', placeholder: 'start', 'data-date-range-picker-target': 'start' %>" +
  #   "\n        <%= date_field_localized f, 'q[0][#{name}_search_end]',  @q[\'0\'][:#{name}_search_end], autocomplete: 'off', size: 40, class: 'form-control', type: 'text', placeholder: 'end' , 'data-date-range-picker-target': 'end'%>" +
  #   "\n     "
end

#spec_list_view_assertionObject



61
62
63
# File 'lib/generators/hot_glue/fields/association_field.rb', line 61

def spec_list_view_assertion

end

#spec_make_assertionObject



53
54
55
# File 'lib/generators/hot_glue/fields/association_field.rb', line 53

def spec_make_assertion
  " expect(page).to have_content(#{assoc}1.name)"
end


65
66
67
68
69
70
71
# File 'lib/generators/hot_glue/fields/association_field.rb', line 65

def spec_related_column_lets
  the_foreign_class = eval(class_name + ".reflect_on_association(:" + assoc + ")").class_name.split("::").last.underscore

  hawk_keys_on_lets = (hawk_keys["#{assoc}_id".to_sym] ? ", #{auth.gsub('current_', '')}: #{auth}": "")

  "  let!(:#{assoc}1) {create(:#{the_foreign_class}" +  hawk_keys_on_lets + ")}"
end

#spec_setup_and_change_act(which_partial) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/generators/hot_glue/fields/association_field.rb', line 44

def spec_setup_and_change_act(which_partial)
  if which_partial == :update_show_only && update_show_only.include?(name)

  else
    "      #{name}_selector = find(\"[name='#{singular}[#{name}]']\").click \n" +
    "      #{name}_selector.first('option', text: #{assoc}1.name).select_option"
  end
end

#spec_setup_let_argObject



57
58
59
# File 'lib/generators/hot_glue/fields/association_field.rb', line 57

def spec_setup_let_arg
  "#{name.to_s.gsub('_id','')}: #{name.to_s.gsub('_id','')}1"
end

#where_query_statementObject



271
272
273
# File 'lib/generators/hot_glue/fields/association_field.rb', line 271

def where_query_statement
  ".where(*#{name}_query)"
end