Class: Scaffolding::Attribute
- Inherits:
-
Object
- Object
- Scaffolding::Attribute
- Defined in:
- lib/scaffolding/attribute.rb
Overview
This class provides helpful methods for determining when and how we should apply logic for each attribute when Super Scaffolding a new model.
For example, we determine which association to use based off of the attribute passed to ‘bin/super-scaffold` as opposed to the models (classes) themselves. Rails has ActiveRecord::Reflection::AssociationReflection, but this is only useful after we’ve declared the associations. Since we haven’t declared the associations in the models yet, we determine the association for the attribute based on its suffix.
i.e. - bin/super-scaffold crud Project tag_ids:super_selectclass_name=Projectsclass_name=Projects::Tag Here, we determine the association for the ‘tag_ids` attribute by its suffix, `_ids`.
Instance Attribute Summary collapse
-
#attribute_index ⇒ Object
Returns the value of attribute attribute_index.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#original_type ⇒ Object
Returns the value of attribute original_type.
-
#scaffolding_type ⇒ Object
Returns the value of attribute scaffolding_type.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #collection_name ⇒ Object
- #default_value ⇒ Object
-
#initialize(attribute_definition, scaffolding_type, attribute_index) ⇒ Attribute
constructor
A new instance of Attribute.
- #is_association? ⇒ Boolean
- #is_belongs_to? ⇒ Boolean
- #is_boolean? ⇒ Boolean
- #is_first_attribute? ⇒ Boolean
- #is_has_many? ⇒ Boolean
- #is_id? ⇒ Boolean
- #is_ids? ⇒ Boolean
- #is_multiple? ⇒ Boolean
-
#is_required? ⇒ Boolean
if this is the first attribute of a newly scaffolded model, that field is required.
-
#is_unscoped? ⇒ Boolean
Sometimes we need all the magic of a ‘*_id` field, but without the scoping stuff.
- #is_vanilla? ⇒ Boolean
- #name_without_id ⇒ Object
- #name_without_id_suffix ⇒ Object
- #name_without_ids ⇒ Object
-
#partial_name ⇒ Object
Field on the show view.
- #special_processing ⇒ Object
- #title_case ⇒ Object
Constructor Details
#initialize(attribute_definition, scaffolding_type, attribute_index) ⇒ Attribute
Returns a new instance of Attribute.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/scaffolding/attribute.rb', line 19 def initialize(attribute_definition, scaffolding_type, attribute_index) parts = attribute_definition.split(":") self.name = parts.shift self.type, self. = (parts) self.scaffolding_type = scaffolding_type self.attribute_index = attribute_index # We mutate `type` within the transformer, so `original_type` allows us # to access what the developer originally passed to bin/super-scaffold. # (Refer to sql_type_to_field_type_mapping in the transformer) self.original_type = type # Ensure `options` is a hash. self. = if .split(",").map { |s| option_name, option_value = s.split("=") [option_name.to_sym, option_value || true] }.to_h else {} end [:label] ||= "label_string" end |
Instance Attribute Details
#attribute_index ⇒ Object
Returns the value of attribute attribute_index.
14 15 16 |
# File 'lib/scaffolding/attribute.rb', line 14 def attribute_index @attribute_index end |
#name ⇒ Object
Returns the value of attribute name.
14 15 16 |
# File 'lib/scaffolding/attribute.rb', line 14 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
14 15 16 |
# File 'lib/scaffolding/attribute.rb', line 14 def @options end |
#original_type ⇒ Object
Returns the value of attribute original_type.
14 15 16 |
# File 'lib/scaffolding/attribute.rb', line 14 def original_type @original_type end |
#scaffolding_type ⇒ Object
Returns the value of attribute scaffolding_type.
14 15 16 |
# File 'lib/scaffolding/attribute.rb', line 14 def scaffolding_type @scaffolding_type end |
#type ⇒ Object
Returns the value of attribute type.
14 15 16 |
# File 'lib/scaffolding/attribute.rb', line 14 def type @type end |
Instance Method Details
#collection_name ⇒ Object
121 122 123 |
# File 'lib/scaffolding/attribute.rb', line 121 def collection_name is_ids? ? name_without_ids : name_without_id.pluralize end |
#default_value ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/scaffolding/attribute.rb', line 177 def default_value case type when "text_field", "password_field", "text_area" "'Alternative String Value'" when "email_field" "'another.email@test.com'" when "phone_field" "'+19053871234'" when "color_picker" "'#47E37F'" end end |
#is_association? ⇒ Boolean
54 55 56 |
# File 'lib/scaffolding/attribute.rb', line 54 def is_association? is_belongs_to? || is_has_many? end |
#is_belongs_to? ⇒ Boolean
58 59 60 |
# File 'lib/scaffolding/attribute.rb', line 58 def is_belongs_to? is_id? && !is_vanilla? end |
#is_boolean? ⇒ Boolean
74 75 76 |
# File 'lib/scaffolding/attribute.rb', line 74 def is_boolean? original_type == "boolean" end |
#is_first_attribute? ⇒ Boolean
44 45 46 |
# File 'lib/scaffolding/attribute.rb', line 44 def is_first_attribute? attribute_index == 0 && scaffolding_type == :crud end |
#is_has_many? ⇒ Boolean
62 63 64 |
# File 'lib/scaffolding/attribute.rb', line 62 def is_has_many? is_ids? && !is_vanilla? end |
#is_id? ⇒ Boolean
84 85 86 |
# File 'lib/scaffolding/attribute.rb', line 84 def is_id? name.match?(/_id$/) end |
#is_ids? ⇒ Boolean
88 89 90 |
# File 'lib/scaffolding/attribute.rb', line 88 def is_ids? name.match?(/_ids$/) end |
#is_multiple? ⇒ Boolean
70 71 72 |
# File 'lib/scaffolding/attribute.rb', line 70 def is_multiple? &.key?(:multiple) || is_has_many? end |
#is_required? ⇒ Boolean
if this is the first attribute of a newly scaffolded model, that field is required.
49 50 51 52 |
# File 'lib/scaffolding/attribute.rb', line 49 def is_required? return false if type == "file_field" [:required] || is_first_attribute? end |
#is_unscoped? ⇒ Boolean
Sometimes we need all the magic of a ‘*_id` field, but without the scoping stuff. Possibly only ever used internally by `join-model`.
80 81 82 |
# File 'lib/scaffolding/attribute.rb', line 80 def is_unscoped? [:unscoped] end |
#is_vanilla? ⇒ Boolean
66 67 68 |
# File 'lib/scaffolding/attribute.rb', line 66 def is_vanilla? &.key?(:vanilla) end |
#name_without_id ⇒ Object
92 93 94 |
# File 'lib/scaffolding/attribute.rb', line 92 def name_without_id name.gsub(/_id$/, "") end |
#name_without_id_suffix ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/scaffolding/attribute.rb', line 100 def name_without_id_suffix if is_ids? name_without_ids elsif is_id? name_without_id else name end end |
#name_without_ids ⇒ Object
96 97 98 |
# File 'lib/scaffolding/attribute.rb', line 96 def name_without_ids name.gsub(/_ids$/, "").pluralize end |
#partial_name ⇒ Object
Field on the show view.
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 170 171 172 173 174 175 |
# File 'lib/scaffolding/attribute.rb', line 126 def partial_name return [:attribute] if [:attribute] case type when "trix_editor", "ckeditor" "html" when "buttons", "super_select", "options", "boolean" if is_ids? "has_many" elsif is_id? "belongs_to" else "option#{"s" if is_multiple?}" end when "cloudinary_image" # TODO: We're preserving cloudinary_image here for backwards compatibility. # Remove it in a future major release. [:height] = 200 "image" when "image" [:height] = 200 "image" when "phone_field" "phone_number" when "date_field" "date" when "date_and_time_field" "date_and_time" when "email_field" "email" when "emoji_field" "text" when "color_picker" "code" when "text_field" "text" when "text_area" "text" when "file_field" "file#{"s" if is_multiple?}" when "password_field" "text" when "number_field" "number" when "address_field" "address" else raise "Invalid field type: #{type}." end end |
#special_processing ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/scaffolding/attribute.rb', line 190 def special_processing case type when "date_field" "assign_date(strong_params, :#{name})" when "date_and_time_field" "assign_date_and_time(strong_params, :#{name})" when "buttons" if is_boolean? "assign_boolean(strong_params, :#{name})" elsif is_multiple? "assign_checkboxes(strong_params, :#{name})" end when "options" if is_multiple? "assign_checkboxes(strong_params, :#{name})" end when "super_select" if is_boolean? "assign_boolean(strong_params, :#{name})" elsif is_multiple? "assign_select_options(strong_params, :#{name})" end end end |
#title_case ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/scaffolding/attribute.rb', line 110 def title_case if is_ids? # user_ids should be 'Users' name_without_ids.humanize.titlecase elsif is_id? && is_vanilla? "#{name.humanize.titlecase} ID" else name.humanize.titlecase end end |