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



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

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

  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
  if model_has_attachment?
    gsub_file migration, "t.attachment", "t.belongs_to"
    insert_into_file migration, indent("t.integer :attachment_version\n", 6), :after => "t.belongs_to :attachment\n"
  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
22
23
24
25
# 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"

  if model_has_attachment?
    gsub_file model_file, "acts_as_content_block", "acts_as_content_block :belongs_to_attachment => true"
  end
end

#create_controller_and_viewsObject



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

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



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

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