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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/generators/cms/content_block/content_block_generator.rb', line 23

def alter_the_migration
  migration = self.class.migration_exists?(File.absolute_path("db/migrate"), "create_#{table_name}")
  gsub_file migration, "create_table", "create_content_table"
  insert_into_file migration, :after => "def change\n" do
    <<-RUBY
    Cms::ContentType.create!(:name => "#{class_name}", :group_name => "#{group_name}")
    RUBY
  end

  # 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
  unless class_name.starts_with?("Cms::")
    gsub_file migration, " do |t|", ", :prefix=>false do |t|"
  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



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

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"
end

#create_controller_and_viewsObject



54
55
56
57
58
# File 'lib/generators/cms/content_block/content_block_generator.rb', line 54

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

#create_routesObject



60
61
62
63
64
65
66
# File 'lib/generators/cms/content_block/content_block_generator.rb', line 60

def create_routes
  if namespaced?
    route "content_blocks :#{file_name.pluralize}"
  else
    route "namespace :cms  do content_blocks :#{file_name.pluralize} end"
  end
end