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, #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?, #has_many_association, #has_many_field, #has_many_form_hash, #integer_form_hash, #is_a_resource?, #label_type, #model_type, #needs_name?, #placeholder_type, #pluralized_name, #resource_class_name, #resource_name, #setup_is_found?, #string_form_hash, #symbolized_attributes, #text_form_hash

Instance Method Details

#add_resource_to_configObject



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

def add_resource_to_config
  if setup_is_found? && 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
# File 'lib/generators/adminpanel/resource/resource_generator.rb', line 19

def change_fields_aliases
  fields.each do |attribute|
    case attribute.split(':').second
    when 'wysiwyg'
      fields.delete(attribute)
      fields << attribute.split(':').first + ':' + 'text'
    end
  end
end

#generate_controllerObject



33
34
35
36
37
# File 'lib/generators/adminpanel/resource/resource_generator.rb', line 33

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

#generate_files_uploadersObject



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

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


55
56
57
58
59
# File 'lib/generators/adminpanel/resource/resource_generator.rb', line 55

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

#generate_migrationObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/generators/adminpanel/resource/resource_generator.rb', line 39

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



29
30
31
# File 'lib/generators/adminpanel/resource/resource_generator.rb', line 29

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


80
81
82
# File 'lib/generators/adminpanel/resource/resource_generator.rb', line 80

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