Class: Field
- Inherits:
-
Object
- Object
- Field
- Defined in:
- lib/generators/hot_glue/fields/field.rb
Direct Known Subclasses
AssociationField, AttachmentField, BooleanField, DateField, DateTimeField, EnumField, FloatField, IntegerField, RelatedSetField, StringField, TextField, TimeField
Instance Attribute Summary collapse
-
#alt_lookup ⇒ Object
Returns the value of attribute alt_lookup.
-
#assoc_class ⇒ Object
Returns the value of attribute assoc_class.
-
#assoc_label ⇒ Object
Returns the value of attribute assoc_label.
-
#assoc_model ⇒ Object
Returns the value of attribute assoc_model.
-
#assoc_name ⇒ Object
Returns the value of attribute assoc_name.
-
#associations ⇒ Object
Returns the value of attribute associations.
-
#attachment_data ⇒ Object
Returns the value of attribute attachment_data.
-
#auth ⇒ Object
Returns the value of attribute auth.
-
#class_name ⇒ Object
Returns the value of attribute class_name.
-
#default_boolean_display ⇒ Object
Returns the value of attribute default_boolean_display.
-
#display_as ⇒ Object
Returns the value of attribute display_as.
-
#form_labels_position ⇒ Object
Returns the value of attribute form_labels_position.
-
#form_placeholder_labels ⇒ Object
Returns the value of attribute form_placeholder_labels.
-
#god ⇒ Object
Returns the value of attribute god.
-
#hawk_keys ⇒ Object
Returns the value of attribute hawk_keys.
-
#hidden_create ⇒ Object
Returns the value of attribute hidden_create.
-
#hidden_update ⇒ Object
Returns the value of attribute hidden_update.
-
#layout_strategy ⇒ Object
Returns the value of attribute layout_strategy.
-
#limit ⇒ Object
Returns the value of attribute limit.
-
#modify_as ⇒ Object
Returns the value of attribute modify_as.
-
#name ⇒ Object
Returns the value of attribute name.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#object ⇒ Object
Returns the value of attribute object.
-
#ownership_field ⇒ Object
Returns the value of attribute ownership_field.
-
#plural ⇒ Object
Returns the value of attribute plural.
-
#polymorphic_parents ⇒ Object
Returns the value of attribute polymorphic_parents.
-
#pundit ⇒ Object
Returns the value of attribute pundit.
-
#sample_file_path ⇒ Object
Returns the value of attribute sample_file_path.
-
#self_auth ⇒ Object
Returns the value of attribute self_auth.
-
#singular ⇒ Object
Returns the value of attribute singular.
-
#singular_class ⇒ Object
Returns the value of attribute singular_class.
-
#sql_type ⇒ Object
Returns the value of attribute sql_type.
-
#stimmify ⇒ Object
Returns the value of attribute stimmify.
-
#update_show_only ⇒ Object
Returns the value of attribute update_show_only.
Instance Method Summary collapse
- #code_to_reset_match_if_search_is_blank ⇒ Object
- #display_boolean_as ⇒ Object
- #field_error_name ⇒ Object
- #field_output(type = nil, width) ⇒ Object
- #field_view_output ⇒ Object
- #form_field_output ⇒ Object
- #form_show_only_output ⇒ Object
- #getName ⇒ Object
- #hidden_output ⇒ Object
-
#initialize(scaffold:, name:) ⇒ Field
constructor
A new instance of Field.
- #label_class ⇒ Object
- #label_for ⇒ Object
- #line_field_output ⇒ Object
- #modified_display_output(show_only: false) ⇒ Object
- #modify_binary? ⇒ Boolean
- #newline_after_field? ⇒ Boolean
- #prelookup_syntax ⇒ Object
- #spec_list_view_assertion ⇒ Object
- #spec_list_view_natural_assertion ⇒ Object
- #spec_make_assertion ⇒ Object
- #spec_random_data ⇒ Object
- #spec_related_column_lets ⇒ Object
- #spec_setup_and_change_act(which_partial = nil) ⇒ Object
- #spec_setup_let_arg ⇒ Object
- #testing_name ⇒ Object
- #text_area_output(field_length, extra_classes: "") ⇒ Object
- #viewable_output ⇒ Object
Constructor Details
#initialize(scaffold:, name:) ⇒ Field
Returns a new instance of Field.
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 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/generators/hot_glue/fields/field.rb', line 13 def initialize( scaffold:, name: ) @name = name @layout_strategy = scaffold.layout_strategy @alt_lookup = scaffold.alt_lookups @singular = scaffold.singular @class_name = scaffold.singular_class @update_show_only = scaffold.update_show_only @hawk_keys = scaffold.hawk_keys @auth = scaffold.auth @sample_file_path = scaffold.sample_file_path @form_placeholder_labels = scaffold.form_placeholder_labels @ownership_field = scaffold.ownership_field @form_labels_position = scaffold.form_labels_position @modify_as = scaffold.modify_as[name.to_sym] # note whenever used as field, don't relookup the key @display_as = scaffold.display_as @pundit = scaffold.pundit @plural = scaffold.plural @self_auth = scaffold.self_auth @default_boolean_display = scaffold.default_boolean_display @namespace = scaffold.namespace_value @stimmify = scaffold.stimmify @hidden_create = scaffold.hidden_create @hidden_update = scaffold.hidden_update @attachment_data = scaffold.[name.to_sym] @god = scaffold.god @polymorphic_parents = scaffold.polymorphic_parents # TODO: remove knowledge of subclasses from Field unless self.class == AttachmentField || self.class == RelatedSetField @sql_type = eval("#{class_name}.columns_hash['#{name}']").sql_type @limit = eval("#{class_name}.columns_hash['#{name}']").limit end end |
Instance Attribute Details
#alt_lookup ⇒ Object
Returns the value of attribute alt_lookup.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def alt_lookup @alt_lookup end |
#assoc_class ⇒ Object
Returns the value of attribute assoc_class.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def assoc_class @assoc_class end |
#assoc_label ⇒ Object
Returns the value of attribute assoc_label.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def assoc_label @assoc_label end |
#assoc_model ⇒ Object
Returns the value of attribute assoc_model.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def assoc_model @assoc_model end |
#assoc_name ⇒ Object
Returns the value of attribute assoc_name.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def assoc_name @assoc_name end |
#associations ⇒ Object
Returns the value of attribute associations.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def associations @associations end |
#attachment_data ⇒ Object
Returns the value of attribute attachment_data.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def @attachment_data end |
#auth ⇒ Object
Returns the value of attribute auth.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def auth @auth end |
#class_name ⇒ Object
Returns the value of attribute class_name.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def class_name @class_name end |
#default_boolean_display ⇒ Object
Returns the value of attribute default_boolean_display.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def default_boolean_display @default_boolean_display end |
#display_as ⇒ Object
Returns the value of attribute display_as.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def display_as @display_as end |
#form_labels_position ⇒ Object
Returns the value of attribute form_labels_position.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def form_labels_position @form_labels_position end |
#form_placeholder_labels ⇒ Object
Returns the value of attribute form_placeholder_labels.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def form_placeholder_labels @form_placeholder_labels end |
#god ⇒ Object
Returns the value of attribute god.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def god @god end |
#hawk_keys ⇒ Object
Returns the value of attribute hawk_keys.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def hawk_keys @hawk_keys end |
#hidden_create ⇒ Object
Returns the value of attribute hidden_create.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def hidden_create @hidden_create end |
#hidden_update ⇒ Object
Returns the value of attribute hidden_update.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def hidden_update @hidden_update end |
#layout_strategy ⇒ Object
Returns the value of attribute layout_strategy.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def layout_strategy @layout_strategy end |
#limit ⇒ Object
Returns the value of attribute limit.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def limit @limit end |
#modify_as ⇒ Object
Returns the value of attribute modify_as.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def modify_as @modify_as end |
#name ⇒ Object
Returns the value of attribute name.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def name @name end |
#namespace ⇒ Object
Returns the value of attribute namespace.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def namespace @namespace end |
#object ⇒ Object
Returns the value of attribute object.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def object @object end |
#ownership_field ⇒ Object
Returns the value of attribute ownership_field.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def ownership_field @ownership_field end |
#plural ⇒ Object
Returns the value of attribute plural.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def plural @plural end |
#polymorphic_parents ⇒ Object
Returns the value of attribute polymorphic_parents.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def polymorphic_parents @polymorphic_parents end |
#pundit ⇒ Object
Returns the value of attribute pundit.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def pundit @pundit end |
#sample_file_path ⇒ Object
Returns the value of attribute sample_file_path.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def sample_file_path @sample_file_path end |
#self_auth ⇒ Object
Returns the value of attribute self_auth.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def self_auth @self_auth end |
#singular ⇒ Object
Returns the value of attribute singular.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def singular @singular end |
#singular_class ⇒ Object
Returns the value of attribute singular_class.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def singular_class @singular_class end |
#sql_type ⇒ Object
Returns the value of attribute sql_type.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def sql_type @sql_type end |
#stimmify ⇒ Object
Returns the value of attribute stimmify.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def stimmify @stimmify end |
#update_show_only ⇒ Object
Returns the value of attribute update_show_only.
2 3 4 |
# File 'lib/generators/hot_glue/fields/field.rb', line 2 def update_show_only @update_show_only end |
Instance Method Details
#code_to_reset_match_if_search_is_blank ⇒ Object
230 231 232 |
# File 'lib/generators/hot_glue/fields/field.rb', line 230 def code_to_reset_match_if_search_is_blank nil end |
#display_boolean_as ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/generators/hot_glue/fields/field.rb', line 209 def display_boolean_as if ! @default_boolean_display @default_boolean_display = "radio" end if display_as[name.to_sym] return display_as[name.to_sym][:boolean] || "radio" else return @default_boolean_display end end |
#field_error_name ⇒ Object
59 60 61 |
# File 'lib/generators/hot_glue/fields/field.rb', line 59 def field_error_name name end |
#field_output(type = nil, width) ⇒ Object
174 175 176 177 178 179 180 181 182 |
# File 'lib/generators/hot_glue/fields/field.rb', line 174 def field_output(type = nil, width ) if modify_as && modify_as[:timezone] "<%= f.time_zone_select :#{name}, ActiveSupport::TimeZone.all, {}, {class: 'form-control'} %>" else parts = name.split('_') camelcase_name = parts.first + parts[1..].map(&:capitalize).join " <%= f.text_field :#{name}, value: #{singular}.#{name}, autocomplete: 'off', size: #{width}, class: 'form-control', type: '#{type}'" + (form_placeholder_labels ? ", placeholder: '#{name.to_s.humanize}'" : "") + (stimmify ? ", 'data-#{@stimmify}-target': '#{camelcase_name}' " : "") + " %>\n " + "\n" end end |
#field_view_output ⇒ Object
126 127 128 129 130 131 132 |
# File 'lib/generators/hot_glue/fields/field.rb', line 126 def field_view_output if modify_as && modify_as[:none] "<span class='badge #{modify_as[:badges]}'>" + field_view_output + "</span>" else "<%= #{singular}.#{name} %>" end end |
#form_field_output ⇒ Object
55 56 57 |
# File 'lib/generators/hot_glue/fields/field.rb', line 55 def form_field_output raise "superclass must implement" end |
#form_show_only_output ⇒ Object
114 115 116 |
# File 'lib/generators/hot_glue/fields/field.rb', line 114 def form_show_only_output viewable_output end |
#getName ⇒ Object
51 52 53 |
# File 'lib/generators/hot_glue/fields/field.rb', line 51 def getName @name end |
#hidden_output ⇒ Object
184 185 186 187 188 189 190 |
# File 'lib/generators/hot_glue/fields/field.rb', line 184 def hidden_output parts = name.split('_') camelcase_name = parts.first + parts[1..].map(&:capitalize).join "<%= f.hidden_field :#{name}, value: #{singular}.#{name} " + (@stimmify ? ", 'data-#{@stimmify}-target': '#{camelcase_name}' " : "") + " %>" end |
#label_class ⇒ Object
222 223 224 |
# File 'lib/generators/hot_glue/fields/field.rb', line 222 def label_class "text-muted small form-text" end |
#label_for ⇒ Object
226 227 228 |
# File 'lib/generators/hot_glue/fields/field.rb', line 226 def label_for end |
#line_field_output ⇒ Object
110 111 112 |
# File 'lib/generators/hot_glue/fields/field.rb', line 110 def line_field_output viewable_output end |
#modified_display_output(show_only: false) ⇒ Object
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 170 171 172 |
# File 'lib/generators/hot_glue/fields/field.rb', line 135 def modified_display_output(show_only: false) res = +'' if modify_as[:cast] && modify_as[:cast] == "$" if name.ends_with?("_cents") res += "<%= number_to_currency(#{singular}.#{name} / 100) %>" else res += "<%= number_to_currency(#{singular}.#{name}) %>" end elsif modify_as[:binary] res += "<%= #{singular}.#{name} ? '#{modify_as[:binary][:truthy]}' : '#{modify_as[:binary][:falsy]}' %>" elsif modify_as[:tinymce] elsif modify_as[:timezone] res += "<%= #{singular}.#{name} %>" elsif modify_as[:urlwrap] # helper_method = modify_as[:helper_method].split(" ")[0] # param = modify_as[:helper_method].split(" ")[1] res += "<%= #{modify_as[:helper_method]}(#{singular}.#{name}, #{singular}) %>" elsif modify_as[:enum] elsif modify_as[:none] field_view_output # res += "<%= render partial: #{singular}.#{name}, locals: {#{singular}: #{singular}} %>" end # if modify_as[:badges] # badge_code = if modify_as[:binary] # "#{singular}.#{name} ? '#{modify_as[:badges].split("|")[0]}' : '#{modify_as[:badges].split("|")[1]}'" # else # modify_as[:badges].split("|").to_s + "[#{singular}.#{name}]" # end # res = "<span class='badge <%= #{badge_code} %>'>" + res + "</span>" # end res end |
#modify_binary? ⇒ Boolean
204 205 206 207 |
# File 'lib/generators/hot_glue/fields/field.rb', line 204 def modify_binary? return false if !modify_as modify_as[:binary] end |
#newline_after_field? ⇒ Boolean
234 235 236 |
# File 'lib/generators/hot_glue/fields/field.rb', line 234 def newline_after_field? false end |
#prelookup_syntax ⇒ Object
238 |
# File 'lib/generators/hot_glue/fields/field.rb', line 238 def prelookup_syntax; nil; end |
#spec_list_view_assertion ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/generators/hot_glue/fields/field.rb', line 97 def spec_list_view_assertion if modify_binary? "expect(page).to have_content('#{modify_as[:binary][:truthy]}'" else spec_list_view_natural_assertion end end |
#spec_list_view_natural_assertion ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/generators/hot_glue/fields/field.rb', line 89 def spec_list_view_natural_assertion if !self_auth "expect(page).to have_content(#{singular}#{1}.#{name})" else "expect(page).to have_content(current_#{singular}.#{name})" end end |
#spec_make_assertion ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/generators/hot_glue/fields/field.rb', line 76 def spec_make_assertion if !modify_binary? "expect(page).to have_content(new_#{name})" else "expect(page).to have_content('#{modify_as[:binary][:truthy]}'" end end |
#spec_random_data ⇒ Object
63 64 65 |
# File 'lib/generators/hot_glue/fields/field.rb', line 63 def spec_random_data end |
#spec_related_column_lets ⇒ Object
106 107 108 |
# File 'lib/generators/hot_glue/fields/field.rb', line 106 def "" end |
#spec_setup_and_change_act(which_partial = nil) ⇒ Object
71 72 73 |
# File 'lib/generators/hot_glue/fields/field.rb', line 71 def spec_setup_and_change_act(which_partial = nil) "" end |
#spec_setup_let_arg ⇒ Object
85 86 87 |
# File 'lib/generators/hot_glue/fields/field.rb', line 85 def spec_setup_let_arg end |
#testing_name ⇒ Object
67 68 69 |
# File 'lib/generators/hot_glue/fields/field.rb', line 67 def testing_name class_name.to_s.gsub("::","_").underscore end |
#text_area_output(field_length, extra_classes: "") ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/generators/hot_glue/fields/field.rb', line 192 def text_area_output(field_length, extra_classes: "") lines = field_length % 40 if lines > 5 lines = 5 end parts = name.split('_') camelcase_name = parts.first + parts[1..].map(&:capitalize).join "<%= f.text_area :#{name}, class: 'form-control#{extra_classes}', autocomplete: 'off', cols: 40, rows: '#{lines}'" + ( form_placeholder_labels ? ", placeholder: '#{name.to_s.humanize}'" : "") + (@stimmify ? ", 'data-#{@stimmify}-target': '#{camelcase_name}' " : "") + " %>" end |
#viewable_output ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/generators/hot_glue/fields/field.rb', line 118 def viewable_output if modify_as modified_display_output(show_only: true) else field_view_output end end |