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_migration ⇒ Object



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
    "    Cms::ContentType.create!(:name => \"\#{class_name}\", :group_name => \"\#{group_name}\")\n    RUBY\n  end\n\n  unless class_name.starts_with?(\"Cms::\")\n    gsub_file migration, \" do |t|\", \", :prefix=>false do |t|\"\n  end\n  self.attributes.select { |attr| attr.type == :category }.each do\n    gsub_file migration, \"t.category\", \"t.belongs_to\"\n  end\n  if model_has_attachment?\n    gsub_file migration, \"t.attachment\", \"t.belongs_to\"\n    insert_into_file migration, indent(\"t.integer :attachment_version\\n\", 6), :after => \"t.belongs_to :attachment\\n\"\n  end\n  self.attributes.select { |attr| attr.type == :html }.each do |attribute|\n    gsub_file migration, \"t.html :\#{attribute.name}\", \"t.text :\#{attribute.name}, :size => (64.kilobytes + 1)\"\n  end\nend\n"

#alter_the_model ⇒ Object



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_views ⇒ Object



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_routes ⇒ Object



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