lib/generators/adminpanel/resource/resource_generator_helper.rb in adminpanel-2.6.1 vs lib/generators/adminpanel/resource/resource_generator_helper.rb in adminpanel-3.0.0

- old
+ new

@@ -9,11 +9,11 @@ def class_name "#{resource_name}_#{@attr_field}".camelize end - def select_field(resource) + def belongs_to_field(resource) "#{resource.singularize.downcase}_id" end def checkbox_field(resource) "#{resource.singularize.downcase}_ids" @@ -33,11 +33,11 @@ end def is_a_resource? fields.each do |attribute| assign_attributes_variables(attribute) - if @attr_type != 'select' + if @attr_type != 'belongs_to' return true end end false end @@ -64,12 +64,12 @@ def symbolized_attributes fields.map do |attribute| assign_attributes_variables(attribute) case @attr_type - when 'select' - ":#{select_field(@attr_field)}" + when 'belongs_to' + ":#{belongs_to_field(@attr_field)}" when 'checkbox' "{ #{checkbox_field(@attr_field)}: [] }" else ":#{attribute.split(':').first}" end @@ -101,10 +101,14 @@ def file_form_hash attribute_hash(@attr_field, 'file_field') end + def image_form_hash + attribute_hash(@attr_field, 'image_field') + end + def boolean_form_hash attribute_hash(@attr_field, 'boolean') end def date_form_hash @@ -113,16 +117,20 @@ def file_field_form_hash attribute_hash(gallery_name.pluralize, 'adminpanel_file_field') end - def select_form_hash - attribute_hash(select_field(@attr_field), 'select', resource_class_name(@attr_field)) + def belongs_to_form_hash + attribute_hash(belongs_to_field(@attr_field), 'select', resource_class_name(@attr_field)) end def checkbox_form_hash - attribute_hash(checkbox_field(resource_class_name(@attr_field.downcase.singularize + 's')), 'checkbox', @attr_field.capitalize.singularize) + attribute_hash( + checkbox_field(resource_class_name(@attr_field.downcase.singularize + 's')), + 'checkbox', + @attr_field.capitalize.singularize + ) end def attribute_hash(name, type, model = '') model = model_type(model) + ",\n" if model != '' "{\n" + @@ -154,15 +162,14 @@ end def has_associations? fields.each do |attribute| assign_attributes_variables(attribute) - if( @attr_type == 'images' || - @attr_type == 'select' || + if( @attr_type == 'belongs_to' || @attr_type == 'checkbox' || @attr_type == 'file' || - has_gallery? ) + has_gallery?) return true end end return false end @@ -170,37 +177,37 @@ def associations association = "" fields.each do |attribute| assign_attributes_variables(attribute) case @attr_type - when 'select' - association = "#{association}#{select_association(@attr_field)}" + when 'belongs_to' + association = "#{association}#{belongs_to_association(@attr_field)}" when 'checkbox' association = "#{association}#{checkbox_association(@attr_field)}" - when 'file' - association = "#{association}#{file_assocation(@attr_field)}" + when 'image', 'file' + association = "#{association}#{file_association(@attr_field)}" end end if has_gallery? association = "#{association}mount_images :#{gallery_name.pluralize}\n\t\t" end association end - def select_association(field) + def belongs_to_association(field) "belongs_to :#{field.singularize.downcase}\n\t\t" end def checkbox_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 - def file_assocation(field) + def file_association(field) "mount_uploader :#{field}, #{class_name}Uploader\n\t\t" end end end