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, #boolean_form_hash, #camelized_resource, #checkbox_association, #checkbox_field, #checkbox_form_hash, #class_name, #datepicker_form_hash, #file_assocation, #file_field_form_hash, #file_form_hash, #float_form_hash, #form_type, #gallery_name, #get_attribute_hash, #has_associations?, #has_gallery?, #integer_form_hash, #is_a_resource?, #label_type, #model_type, #needs_name?, #placeholder_type, #pluralized_name, #resource_class_name, #resource_name, #select_association, #select_field, #select_form_hash, #string_form_hash, #symbolized_attributes, #text_form_hash

Instance Method Details

#add_resource_to_configObject



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

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
36
37
# File 'lib/generators/adminpanel/resource/resource_generator.rb', line 19

def change_fields_aliases
  fields_to_delete = []
  fields.each do |attribute|
    case attribute.split(':').second
    when 'wysiwyg'
      fields_to_delete << attribute
      fields << attribute.split(':').first + ':' + 'text'
    when 'belongs_to'
      fields_to_delete << attribute
      fields << attribute.split(':').first + ':' + 'select'
    when 'has_many'
      fields_to_delete << attribute
      fields << attribute.split(':').first + ':' + 'checkbox'
    end
  end
  fields_to_delete.each do |field|
    fields.delete(field)
  end
end

#generate_controllerObject



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

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

#generate_files_uploadersObject



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

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


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

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

#generate_migrationObject



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

def generate_migration
  parameters = fields.dup
  parameters.delete_if do |pair|
    pair.split(':').second == 'has_many'
  end
  parameters.each do |attribute|
    case attribute.split(':').second
    when 'file'
      parameters.delete(attribute)
      parameters << attribute.split(':').first + ':' + 'string'
    end
  end
  parameters << 'created_at:datetime' << 'updated_at:datetime'
  invoke :migration, ["create_adminpanel_#{pluralized_name}", parameters]
end

#generate_modelObject



39
40
41
# File 'lib/generators/adminpanel/resource/resource_generator.rb', line 39

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


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

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