Module: Adminpanel::ResourceGeneratorHelper
- Included in:
- MigrationGenerator, ResourceGenerator
- Defined in:
- lib/generators/adminpanel/resource/resource_generator_helper.rb
Instance Method Summary collapse
- #assign_attributes_variables(attribute) ⇒ Object
- #associations ⇒ Object
- #attribute_hash(name, type, model = '') ⇒ Object
- #belongs_to_association(field) ⇒ Object
- #belongs_to_field(resource) ⇒ Object
- #belongs_to_form_hash ⇒ Object
- #boolean_form_hash ⇒ Object
- #camelized_resource ⇒ Object
- #class_name ⇒ Object
- #datepicker_form_hash ⇒ Object
- #file_assocation(field) ⇒ Object
- #file_field_form_hash ⇒ Object
- #file_form_hash ⇒ Object
- #float_form_hash ⇒ Object
- #form_type(type) ⇒ Object
- #gallery_name ⇒ Object
- #get_attribute_hash ⇒ Object
- #has_associations? ⇒ Boolean
- #has_gallery? ⇒ Boolean
- #has_many_association(field) ⇒ Object
- #has_many_field(resource) ⇒ Object
- #has_many_form_hash ⇒ Object
- #integer_form_hash ⇒ Object
- #is_a_resource? ⇒ Boolean
- #label_type ⇒ Object
- #model_type(type) ⇒ Object
- #needs_name? ⇒ Boolean
- #placeholder_type ⇒ Object
- #pluralized_name ⇒ Object
- #resource_class_name(resource) ⇒ Object
- #resource_name ⇒ Object
- #setup_is_found? ⇒ Boolean
- #string_form_hash ⇒ Object
- #symbolized_attributes ⇒ Object
- #text_form_hash ⇒ Object
Instance Method Details
#assign_attributes_variables(attribute) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 34 def assign_attributes_variables(attribute) @attr_field = attribute.split(":").first if attribute.split(":").second.nil? @attr_type = "string" else @attr_type = attribute.split(":").second end end |
#associations ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 177 def associations association = "" fields.each do |attribute| assign_attributes_variables(attribute) case @attr_type when 'belongs_to' association = "#{association}#{belongs_to_association(@attr_field)}" when 'has_many' || 'has_many_through' association = "#{association}#{has_many_association(@attr_field)}" when 'file' association = "#{association}#{file_assocation(@attr_field)}" end end if has_gallery? association = "#{association}mount_images :#{gallery_name.pluralize}\n\t\t" end association end |
#attribute_hash(name, type, model = '') ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 134 def attribute_hash(name, type, model = '') model = model_type(model) + ",\n" if model != '' "{\n" + indent("'#{name}'" + " => {\n", 2) + indent(form_type(type), 4) + ",\n" + indent(label_type, 4) + ",\n" + indent(placeholder_type, 4) + ",\n" + indent(model, 4) + indent("}\n", 2) + '}' end |
#belongs_to_association(field) ⇒ Object
198 199 200 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 198 def belongs_to_association(field) "belongs_to :#{field.singularize.downcase}\n\t\t" end |
#belongs_to_field(resource) ⇒ Object
22 23 24 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 22 def belongs_to_field(resource) "#{resource.singularize.downcase}_id" end |
#belongs_to_form_hash ⇒ Object
126 127 128 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 126 def belongs_to_form_hash attribute_hash(belongs_to_field(@attr_field), 'belongs_to', resource_class_name(@attr_field)) end |
#boolean_form_hash ⇒ Object
114 115 116 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 114 def boolean_form_hash attribute_hash(@attr_field, 'boolean') end |
#camelized_resource ⇒ Object
65 66 67 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 65 def camelized_resource resource_name.camelize end |
#class_name ⇒ Object
18 19 20 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 18 def class_name "#{resource_name}_#{@attr_field}".camelize end |
#datepicker_form_hash ⇒ Object
118 119 120 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 118 def datepicker_form_hash attribute_hash(@attr_field, 'datepicker') end |
#file_assocation(field) ⇒ Object
209 210 211 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 209 def file_assocation(field) return "mount_uploader :#{field}, #{class_name}Uploader\n\t\t" end |
#file_field_form_hash ⇒ Object
122 123 124 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 122 def file_field_form_hash attribute_hash(gallery_name.pluralize, 'adminpanel_file_field') end |
#file_form_hash ⇒ Object
110 111 112 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 110 def file_form_hash attribute_hash(@attr_field, 'file_field') end |
#float_form_hash ⇒ Object
98 99 100 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 98 def float_form_hash attribute_hash(@attr_field, 'text_field') end |
#form_type(type) ⇒ Object
146 147 148 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 146 def form_type(type) "'type' => '#{type}'" end |
#gallery_name ⇒ Object
61 62 63 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 61 def gallery_name "#{resource_name}file" #ex: postfile end |
#get_attribute_hash ⇒ Object
87 88 89 90 91 92 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 87 def get_attribute_hash fields.map do |attribute| assign_attributes_variables(attribute) send(@attr_type + '_form_hash') end.join(", \n") end |
#has_associations? ⇒ Boolean
162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 162 def has_associations? fields.each do |attribute| assign_attributes_variables(attribute) if( @attr_type == 'images' || @attr_type == 'belongs_to' || @attr_type == 'has_many' || @attr_type == 'has_many_through' || @attr_type == 'file' || has_gallery? ) return true end end return false end |
#has_gallery? ⇒ Boolean
53 54 55 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 53 def has_gallery? ![:'skip-gallery'] end |
#has_many_association(field) ⇒ Object
202 203 204 205 206 207 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 202 def has_many_association(field) return "# has_many :#{@attr_field.downcase}zations\n\t\t" + "# has_many :#{@attr_field.pluralize.downcase}, " + "through: :#{@attr_field.downcase}zations, " + "dependent: :destroy\n\t\t" end |
#has_many_field(resource) ⇒ Object
26 27 28 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 26 def has_many_field(resource) "#{resource.singularize.downcase}_ids" end |
#has_many_form_hash ⇒ Object
130 131 132 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 130 def has_many_form_hash attribute_hash(has_many_field(resource_class_name(@attr_field.downcase.singularize + 's')), 'has_many', @attr_field.capitalize.singularize) end |
#integer_form_hash ⇒ Object
106 107 108 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 106 def integer_form_hash attribute_hash(@attr_field, 'number_field') end |
#is_a_resource? ⇒ Boolean
43 44 45 46 47 48 49 50 51 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 43 def is_a_resource? fields.each do |attribute| assign_attributes_variables(attribute) if @attr_type != 'belongs_to' return true end end false end |
#label_type ⇒ Object
150 151 152 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 150 def label_type "'label' => '#{@attr_field}'" end |
#model_type(type) ⇒ Object
158 159 160 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 158 def model_type(type) "'model' => 'Adminpanel::#{type}'" end |
#needs_name? ⇒ Boolean
11 12 13 14 15 16 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 11 def needs_name? fields.each do |attribute| return false if attribute.split(':').first == 'name' end true end |
#placeholder_type ⇒ Object
154 155 156 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 154 def placeholder_type "'placeholder' => '#{@attr_field}'" end |
#pluralized_name ⇒ Object
69 70 71 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 69 def pluralized_name "#{resource_name.pluralize}" end |
#resource_class_name(resource) ⇒ Object
30 31 32 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 30 def resource_class_name(resource) "#{resource.singularize.capitalize}" end |
#resource_name ⇒ Object
57 58 59 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 57 def resource_name name.singularize.downcase #normalize name to downcase and singular end |
#setup_is_found? ⇒ Boolean
3 4 5 6 7 8 9 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 3 def setup_is_found? if Dir.exists?('config') && Dir.exists?('config/initializers') && File.exists?('config/initializers/adminpanel_setup.rb') true else false end end |
#string_form_hash ⇒ Object
94 95 96 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 94 def string_form_hash attribute_hash(@attr_field, 'text_field') end |
#symbolized_attributes ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 73 def symbolized_attributes fields.map do |attribute| assign_attributes_variables(attribute) case @attr_type when 'belongs_to' ':' + belongs_to_field(@attr_field) when 'has_many' "{ #{has_many_field(@attr_field)}: [] }" else ":#{attribute.split(':').first}" end end.join(",\n") end |
#text_form_hash ⇒ Object
102 103 104 |
# File 'lib/generators/adminpanel/resource/resource_generator_helper.rb', line 102 def text_form_hash attribute_hash(@attr_field ,'wysiwyg_field') end |