Class | Cms::ContentBlockController |
In: |
app/controllers/cms/content_block_controller.rb
|
Parent: | Cms::BaseController |
This is not called directly This is the base class for other content blocks
# File app/controllers/cms/content_block_controller.rb, line 29 29: def create 30: if create_block 31: after_create_on_success 32: else 33: after_create_on_failure 34: end 35: rescue Exception => @exception 36: after_create_on_error 37: end
# File app/controllers/cms/content_block_controller.rb, line 56 56: def destroy 57: do_command("deleted") { @block.destroy } 58: redirect_to_first params[:_redirect_to], blocks_path 59: end
# File app/controllers/cms/content_block_controller.rb, line 39 39: def edit 40: load_block_draft 41: render "#{template_directory}/edit" 42: end
Basic REST Crud Action
# File app/controllers/cms/content_block_controller.rb, line 13 13: def index 14: load_blocks 15: render "#{template_directory}/index" 16: end
# File app/controllers/cms/content_block_controller.rb, line 23 23: def new 24: build_block 25: set_default_category 26: render "#{template_directory}/new" 27: end
Additional CMS Action
# File app/controllers/cms/content_block_controller.rb, line 63 63: def publish 64: do_command("published") { @block.publish! } 65: redirect_to_first params[:_redirect_to], block_path 66: end
# File app/controllers/cms/content_block_controller.rb, line 68 68: def revert_to 69: do_command("reverted to version #{params[:version]}") do 70: revert_block(params[:version]) 71: end 72: redirect_to_first params[:_redirect_to], block_path 73: end
# File app/controllers/cms/content_block_controller.rb, line 18 18: def show 19: load_block_draft 20: render "#{template_directory}/show" 21: end
# File app/controllers/cms/content_block_controller.rb, line 44 44: def update 45: if update_block 46: after_update_on_success 47: else 48: after_update_on_failure 49: end 50: rescue ActiveRecord::StaleObjectError => @exception 51: after_update_on_edit_conflict 52: rescue Exception => @exception 53: after_update_on_error 54: end
# File app/controllers/cms/content_block_controller.rb, line 92 92: def usages 93: load_block_draft 94: @pages = @block.connected_pages.all(:order => 'name') 95: render "#{template_directory}/usages" 96: end
# File app/controllers/cms/content_block_controller.rb, line 75 75: def version 76: load_block 77: if params[:version] 78: @block = @block.as_of_version(params[:version]) 79: end 80: render "#{template_directory}/show" 81: end
# File app/controllers/cms/content_block_controller.rb, line 83 83: def versions 84: if model_class.versioned? 85: load_block 86: render "#{template_directory}/versions" 87: else 88: render :text => "Not Implemented", :status => :not_implemented 89: end 90: end
# File app/controllers/cms/content_block_controller.rb, line 194 194: def after_create_on_error 195: logger.error "#{@exception.message}\n#{@exception.backtrace.join('\n')}" 196: after_create_on_failure 197: end
# File app/controllers/cms/content_block_controller.rb, line 190 190: def after_create_on_failure 191: render "#{template_directory}/new" 192: end
# File app/controllers/cms/content_block_controller.rb, line 180 180: def after_create_on_success 181: block = @block.class.versioned? ? @block.draft : @block 182: flash[:notice] = "#{content_type.display_name} '#{block.name}' was created" 183: if @block.class.connectable? && @block.connected_page 184: redirect_to @block.connected_page.path 185: else 186: redirect_to_first params[:_redirect_to], block_path 187: end 188: end
# File app/controllers/cms/content_block_controller.rb, line 214 214: def after_update_on_edit_conflict 215: @other_version = @block.class.find(@block.id) 216: after_update_on_failure 217: end
# File app/controllers/cms/content_block_controller.rb, line 219 219: def after_update_on_error 220: logger.error "#{@exception.message}\n#{@exception.backtrace.join('\n')}" 221: after_update_on_failure 222: end
# File app/controllers/cms/content_block_controller.rb, line 210 210: def after_update_on_failure 211: render "#{template_directory}/edit" 212: end
# File app/controllers/cms/content_block_controller.rb, line 205 205: def after_update_on_success 206: flash[:notice] = "#{content_type_name.titleize} '#{@block.name}' was updated" 207: redirect_to_first params[:_redirect_to], block_path 208: end
This is the partial that will be used in the form
# File app/controllers/cms/content_block_controller.rb, line 157 157: def block_form 158: @content_type.form 159: end
# File app/controllers/cms/content_block_controller.rb, line 147 147: def block_path(action=nil) 148: path = [:cms, @block] 149: action ? path.unshift(action) : path 150: end
# File app/controllers/cms/content_block_controller.rb, line 152 152: def blocks_path(options={}) 153: cms_index_url_for(@block, options) 154: end
# File app/controllers/cms/content_block_controller.rb, line 105 105: def content_type 106: @content_type ||= ContentType.find_by_key(content_type_name) 107: end
methods that are used to detemine what content block type we are dealing with
# File app/controllers/cms/content_block_controller.rb, line 101 101: def content_type_name 102: self.class.name.sub(/Controller/,'').demodulize.singularize 103: end
# File app/controllers/cms/content_block_controller.rb, line 254 254: def determine_layout 255: 'cms/content_library' 256: end
A "command" is when you want to perform an action on a content block You pass a ruby block to this method, this calls it and then sets a flash message based on success or failure
# File app/controllers/cms/content_block_controller.rb, line 229 229: def do_command(result) 230: load_block 231: if yield 232: flash[:notice] = "#{content_type_name.titleize} '#{@block.name}' was #{result}" 233: else 234: flash[:error] = "#{content_type_name.titleize} '#{@block.name}' could not be #{result}" 235: end 236: end
# File app/controllers/cms/content_block_controller.rb, line 132 132: def load_block 133: @block = model_class.find(params[:id]) 134: end
# File app/controllers/cms/content_block_controller.rb, line 136 136: def load_block_draft 137: load_block 138: @block = @block.as_of_draft_version if model_class.versioned? 139: end
methods for loading one or a collection of blocks
# File app/controllers/cms/content_block_controller.rb, line 119 119: def load_blocks 120: options = {} 121: if params[:section_id] && params[:section_id] != 'all' 122: options[:include] = { :attachment => { :section_node => :section }} 123: options[:conditions] = ["sections.id = ?", params[:section_id]] 124: end 125: options[:page] = params[:page] 126: options[:order] = model_class.default_order if model_class.respond_to?(:default_order) 127: options[:order] = params[:order] unless params[:order].blank? 128: scope = model_class.respond_to?(:list) ? model_class.list : model_class 129: @blocks = scope.searchable? ? scope.search(params[:search]).paginate(options) : scope.paginate(options) 130: end
# File app/controllers/cms/content_block_controller.rb, line 109 109: def model_class 110: content_type.model_class 111: end
# File app/controllers/cms/content_block_controller.rb, line 113 113: def model_name 114: model_class.name.underscore.to_sym 115: end
path related methods - available in the view as helpers
# File app/controllers/cms/content_block_controller.rb, line 143 143: def new_block_path(options={}) 144: cms_new_url_for(@block, options) 145: end
# File app/controllers/cms/content_block_controller.rb, line 238 238: def revert_block(to_version) 239: begin 240: @block.revert_to(to_version) 241: rescue Exception => @exception 242: logger.warn "Could not revert #{@block.inspect} to version #{to_version}" 243: logger.warn "#{@exception.message}\n:#{@exception.backtrace.join("\n")}" 244: false 245: end 246: end
# File app/controllers/cms/content_block_controller.rb, line 167 167: def set_default_category 168: if @last_block = model_class.last 169: @block.category = @last_block.category if @block.respond_to?(:category=) 170: end 171: end
methods to setup the view
# File app/controllers/cms/content_block_controller.rb, line 250 250: def set_toolbar_tab 251: @toolbar_tab = :content_library 252: end
# File app/controllers/cms/content_block_controller.rb, line 258 258: def template_directory 259: "cms/blocks" 260: end