Class: Cms::Generators::ContentBlockGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
Rails::Generators::Migration, Rails::Generators::ResourceHelpers
Defined in:
lib/generators/cms/content_block/content_block_generator.rb

Overview

Allows developers to create new Content Blocks for their projects.

Instance Method Summary collapse

Instance Method Details

#alter_the_migrationObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/generators/cms/content_block/content_block_generator.rb', line 40

def alter_the_migration
  migration = self.class.migration_exists?(File.absolute_path("db/migrate"), "create_#{table_name}")

  if @in_core_application
    gsub_file migration, "create_table :#{table_name}", "create_table :#{unnamespaced_table_name}"
  end

  gsub_file migration, "create_table", "create_content_table"


  # Attachments do not require a FK from this model to attachments.
  self.attributes.select { |attr| attr.type == :attachment }.each do |attribute|
    gsub_file migration, "t.attachment :#{attribute.name}", ""
  end
  self.attributes.select { |attr| attr.type == :attachments }.each do |attribute|
    gsub_file migration, "t.attachments :#{attribute.name}", ""
  end
  self.attributes.select { |attr| attr.type == :category }.each do
    gsub_file migration, "t.category", "t.belongs_to"
  end
  self.attributes.select { |attr| attr.type == :html }.each do |attribute|
    gsub_file migration, "t.html :#{attribute.name}", "t.text :#{attribute.name}, :size => (64.kilobytes + 1)"
  end
end

#alter_the_modelObject



33
34
35
36
37
38
# File 'lib/generators/cms/content_block/content_block_generator.rb', line 33

def alter_the_model
  model_file = File.join('app/models', class_path, "#{file_name}.rb")
  spaces = namespaced? ? 4 : 2
  insert_into_file model_file, indent("acts_as_content_block\n", spaces), :after => "ActiveRecord::Base\n"
  insert_into_file model_file, indent("content_module :#{file_name.pluralize}\n", spaces), :after => "acts_as_content_block\n"
end

#create_controller_and_viewsObject



70
71
72
73
74
# File 'lib/generators/cms/content_block/content_block_generator.rb', line 70

def create_controller_and_views
  gsub_file File.join('app/controllers', class_path, "#{file_name.pluralize}_controller.rb"), /ApplicationController/, "Cms::ContentBlockController"
  template '_form.html.erb', File.join('app/views', class_path, file_name.pluralize, "_form.html.erb")
  template 'render.html.erb', File.join('app/views', class_path, file_name.pluralize, "render.html.erb")
end

#create_routesObject



76
77
78
79
80
81
82
# File 'lib/generators/cms/content_block/content_block_generator.rb', line 76

def create_routes
  if namespaced? && !@in_core_application
    route "content_blocks :#{file_name.pluralize}"
  else
    route "namespace :#{namespace.name.underscore} do content_blocks :#{file_name.pluralize} end"
  end
end

#generate_controllerObject



24
25
26
27
28
29
# File 'lib/generators/cms/content_block/content_block_generator.rb', line 24

def generate_controller
  application_controller = File.join('app/controllers', class_path, "application_controller.rb")
  unless File.exists?(application_controller)
    template 'application_controller.rb.erb', application_controller
  end
end

#set_classpathObject



15
16
17
18
19
20
21
22
# File 'lib/generators/cms/content_block/content_block_generator.rb', line 15

def set_classpath
  @in_core_application = false
  unless namespaced?
    cms_engine_module_name = Cms::EngineAware.module_name(Rails.application.class).constantize
    Rails::Generators.namespace = cms_engine_module_name
    @in_core_application = true
  end
end