require 'rails/generators' module Pulitzer class GeneratorElementType def initialize(element_type) case element_type.class.name when "Pulitzer::PostTypeContentElementType" @label = element_type.label @kind = 'content_element' when 'Pulitzer::FreeFormSectionType' @label = element_type.name @kind = 'free_form_section' end end def injection_content injection_content_method = "#{@kind}_injection_content" send injection_content_method end def content_element_injection_content <<-VIEW <%= render_cms_element @post.content_element('#{@label}') %> VIEW end def free_form_section_injection_content <<-VIEW <%= render_cms_section(@post, '#{@label}') %> VIEW end end class PostTypeGenerator < Rails::Generators::Base include ActiveSupport::Inflector source_root File.expand_path("../templates", __FILE__) argument :post_type_name, required: true, desc: "The name of the post type." def build_post_type_files abort("Couldn't find Pulitzer::PostType named #{post_type_name}") unless post_type.is_a? Pulitzer::PostType kind_method = "build_#{kind}" send kind_method end protected def build_template route get_route inject_into_file File.join(Rails.root,'config','routes.rb'), after: "namespace :#{Pulitzer.preview_namespace} do\n", force: false do "\t\t#{get_route}\n" end copy_file "post_view.html.erb", view_path all_element_types.each do |el_type| inject_into_file view_path, before: "<%# END Auto-generated by Pulitzer %>", force: false do el_type.injection_content end end inject_into_file File.join(Rails.root,'app','controllers', "#{Pulitzer.public_controller}_controller.rb"), after: "#Pulitzer Generated Actions\n", force: false do get_controller_action('active') end inject_into_file File.join(Rails.root,'app','controllers', Pulitzer.preview_namespace, "#{Pulitzer.public_controller}_controller.rb"), after: "#Pulitzer Generated Actions\n", force: false do get_controller_action('preview') end end def build_partial empty_directory partial_path layouts.each do |layout_name| copy_file "post_view.html.erb", layout_path(layout_name) inject_into_file layout_path(layout_name), before: "<%# END Auto-generated by Pulitzer %>", force: false do <<-BEGIN_COMMENT BEGIN_COMMENT end content_element_names.each do |content_element_name| inject_into_file layout_path(layout_name), before: "<%# END Auto-generated by Pulitzer %>", force: false do <<-VIEW <%= render_cms_element partial.content_element('#{content_element_name}') %> VIEW end end inject_into_file layout_path(layout_name), before: "<%# END Auto-generated by Pulitzer %>", force: false do <<-BEGIN_COMMENT BEGIN_COMMENT end end end def file_name parameterize(post_type_name) end def action_name file_name.gsub(/-/,'_') end def view_path File.join(Rails.root,'app','views', Pulitzer.public_controller, "#{action_name}.html.erb") end def layout_short_path(layout_name) File.join(action_name, "_#{layout_name}.html.erb") end def layout_path(layout_name) File.join(Rails.root,'app','views', Pulitzer.partial_folder, action_name, "_#{layout_name}.html.erb") end def post_type Pulitzer::PostType.named(post_type_name) end def kind post_type.kind end def partial_path File.join(Rails.root,'app','views', Pulitzer.partial_folder, action_name) end def layouts post_type.layouts.map{|l| l.name.gsub(/ /,'_')} end def free_form_section_types post_type.free_form_section_types end def section_names free_form_section_types.map(&:name) end def post_type_content_element_types post_type.post_type_content_element_types end def all_element_types post_type.all_element_types.sort_by(&:sort_order).map do |et| GeneratorElementType.new(et) end end def content_element_names post_type_content_element_types.map(&:label) end def arity return @arity if @arity.present? @arity = post_type.plural? ? 'plural' : 'singleton' puts "Generating files for a #{arity} post type" @arity end def get_route send "#{arity}_route" end def plural_route "get '#{file_name}/:slug' => '#{Pulitzer.public_controller}##{action_name}', as: '#{action_name}'" end def singleton_route "get '#{file_name}' => 'pages##{action_name}', as: '#{action_name}'" end def get_controller_action(context) send "#{arity}_action", context end def plural_action(context) <<-PACTION def #{action_name} @post_type_version = Pulitzer::PostType.named('#{post_type_name}').published_type_version version_number = @post_type_version.version_number @post = @post_type_version.posts.find_by!(slug: params[:slug]).get_#{context}_version! render template: pulitzer_view_path(version_number) end PACTION end def singleton_action(context) <<-CACTION def #{action_name} @post_type_version = Pulitzer::PostType.named('#{post_type_name}').published_type_version version_number = @post_type_version.version_number @post = @post_type_version.singleton_post.get_#{context}_version! render template: pulitzer_view_path(version_number) end CACTION end end end