Class: Adminpanel::ResourceGenerator

Inherits:
ActiveRecord::Generators::Base
  • Object
show all
Includes:
ResourceGeneratorHelper
Defined in:
lib/generators/adminpanel/resource/resource_generator.rb

Instance Method Summary collapse

Methods included from ResourceGeneratorHelper

#assign_attributes_variables, #associations, #attribute_hash, #belongs_to_association, #belongs_to_field, #belongs_to_form_hash, #boolean_form_hash, #camelized_resource, #checkbox_association, #checkbox_field, #checkbox_form_hash, #class_name, #date_form_hash, #file_association, #file_field_form_hash, #file_form_hash, #float_form_hash, #form_attributes_hash, #form_type, #gallery_name, #has_associations?, #has_gallery?, #image_form_hash, #integer_form_hash, #is_a_resource?, #label_type, #model_type, #needs_name?, #placeholder_type, #pluralized_name, #resource_class_name, #resource_name, #string_form_hash, #symbolized_attributes, #text_form_hash

Instance Method Details

#add_resource_to_configObject



80
81
82
83
84
85
86
87
# File 'lib/generators/adminpanel/resource/resource_generator.rb', line 80

def add_resource_to_config
  if is_a_resource?
    inject_into_file 'config/initializers/adminpanel_setup.rb',
      after: 'config.displayable_resources = [' do
      indent "\n:#{pluralized_name},", 4
    end
  end
end

#change_fields_aliasesObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/generators/adminpanel/resource/resource_generator.rb', line 19

def change_fields_aliases
  attrs_to_delete = []
  fields.each do |attribute|
    case attribute.split(':').second
    when 'wysiwyg'
      attrs_to_delete << attribute
      fields << attribute.split(':').first + ':' + 'text'
    when 'datepicker'
      attrs_to_delete << attribute
      fields << attribute.split(':').first + ':' + 'date'
    when 'has_many'
      attrs_to_delete << attribute
      fields << attribute.split(':').first + ':' + 'checkbox'
    end
  end
  attrs_to_delete.each { |f| fields.delete(f) }
end

#generate_controllerObject



41
42
43
44
45
# File 'lib/generators/adminpanel/resource/resource_generator.rb', line 41

def generate_controller
  if is_a_resource?
    template 'adminpanel_controller_template.rb', "app/controllers/adminpanel/#{pluralized_name}_controller.rb"
  end
end

#generate_files_uploadersObject



70
71
72
73
74
75
76
77
78
# File 'lib/generators/adminpanel/resource/resource_generator.rb', line 70

def generate_files_uploaders
  fields.each do |attribute|
    assign_attributes_variables(attribute)
    case @attr_type
    when 'file', 'image'
      template '../../gallery/templates/uploader.rb', "app/uploaders/adminpanel/#{class_name.underscore}_uploader.rb"
    end
  end
end


64
65
66
67
68
# File 'lib/generators/adminpanel/resource/resource_generator.rb', line 64

def generate_gallery
  if has_gallery? && is_a_resource?
    invoke 'adminpanel:gallery', [resource_name]
  end
end

#generate_migrationObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/generators/adminpanel/resource/resource_generator.rb', line 47

def generate_migration
  parameters = fields.dup
  parameters.delete_if { |pair| pair.split(':').second == 'checkbox' }
  attrs_to_delete = []
  parameters.each do |attribute|
    case attribute.split(':').second
    when 'file', 'image'
      attrs_to_delete << attribute
      parameters << attribute.split(':').first + ':' + 'string'
    end
  end
  attrs_to_delete.each { |a| parameters.delete(a) }
  parameters << 'created_at:datetime' << 'updated_at:datetime'

  invoke :migration, ["create_adminpanel_#{pluralized_name}", parameters]
end

#generate_modelObject



37
38
39
# File 'lib/generators/adminpanel/resource/resource_generator.rb', line 37

def generate_model
  template 'adminpanel_resource_template.rb', "app/models/adminpanel/#{resource_name}.rb"
end


89
90
91
# File 'lib/generators/adminpanel/resource/resource_generator.rb', line 89

def print_messages
  puts "don't forget to restart your server and migrate db"
end