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: raise @exception if @exception.is_a?(Cms::Errors::AccessDenied) 37: after_create_on_error 38: end
# File app/controllers/cms/content_block_controller.rb, line 58 58: def destroy 59: do_command("deleted") { @block.destroy } 60: redirect_to_first params[:_redirect_to], blocks_path 61: end
# File app/controllers/cms/content_block_controller.rb, line 40 40: def edit 41: load_block_draft 42: render "#{template_directory}/edit" 43: 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 65 65: def publish 66: do_command("published") { @block.publish! } 67: redirect_to_first params[:_redirect_to], block_path 68: end
# File app/controllers/cms/content_block_controller.rb, line 70 70: def revert_to 71: do_command("reverted to version #{params[:version]}") do 72: revert_block(params[:version]) 73: end 74: redirect_to_first params[:_redirect_to], block_path 75: 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 45 45: def update 46: if update_block 47: after_update_on_success 48: else 49: after_update_on_failure 50: end 51: rescue ActiveRecord::StaleObjectError => @exception 52: after_update_on_edit_conflict 53: rescue Exception => @exception 54: raise @exception if @exception.is_a?(Cms::Errors::AccessDenied) 55: after_update_on_error 56: end
# File app/controllers/cms/content_block_controller.rb, line 94 94: def usages 95: load_block_draft 96: @pages = @block.connected_pages.all(:order => 'name') 97: render "#{template_directory}/usages" 98: end
# File app/controllers/cms/content_block_controller.rb, line 77 77: def version 78: load_block 79: if params[:version] 80: @block = @block.as_of_version(params[:version]) 81: end 82: render "#{template_directory}/show" 83: end
# File app/controllers/cms/content_block_controller.rb, line 85 85: def versions 86: if model_class.versioned? 87: load_block 88: render "#{template_directory}/versions" 89: else 90: render :text => "Not Implemented", :status => :not_implemented 91: end 92: end
# File app/controllers/cms/content_block_controller.rb, line 200 200: def after_create_on_error 201: logger.error "#{@exception.message}\n#{@exception.backtrace.join('\n')}" 202: after_create_on_failure 203: end
# File app/controllers/cms/content_block_controller.rb, line 196 196: def after_create_on_failure 197: render "#{template_directory}/new" 198: end
# File app/controllers/cms/content_block_controller.rb, line 186 186: def after_create_on_success 187: block = @block.class.versioned? ? @block.draft : @block 188: flash[:notice] = "#{content_type.display_name} '#{block.name}' was created" 189: if @block.class.connectable? && @block.connected_page 190: redirect_to @block.connected_page.path 191: else 192: redirect_to_first params[:_redirect_to], block_path 193: end 194: end
# File app/controllers/cms/content_block_controller.rb, line 220 220: def after_update_on_edit_conflict 221: @other_version = @block.class.find(@block.id) 222: after_update_on_failure 223: end
# File app/controllers/cms/content_block_controller.rb, line 225 225: def after_update_on_error 226: logger.error "#{@exception.message}\n#{@exception.backtrace.join('\n')}" 227: after_update_on_failure 228: end
# File app/controllers/cms/content_block_controller.rb, line 216 216: def after_update_on_failure 217: render "#{template_directory}/edit" 218: end
# File app/controllers/cms/content_block_controller.rb, line 211 211: def after_update_on_success 212: flash[:notice] = "#{content_type_name.titleize} '#{@block.name}' was updated" 213: redirect_to_first params[:_redirect_to], block_path 214: end
This is the partial that will be used in the form
# File app/controllers/cms/content_block_controller.rb, line 162 162: def block_form 163: @content_type.form 164: end
# File app/controllers/cms/content_block_controller.rb, line 152 152: def block_path(action=nil) 153: path = [:cms, @block] 154: action ? path.unshift(action) : path 155: end
# File app/controllers/cms/content_block_controller.rb, line 157 157: def blocks_path(options={}) 158: cms_index_url_for(@block, options) 159: end
Use a "whitelist" approach to access to avoid mistakes By default everyone can create new block and view them and their properties, but blocks can only be modified based on the permissions of the pages they are connected to.
# File app/controllers/cms/content_block_controller.rb, line 258 258: def check_permissions 259: case action_name 260: when "index", "show", "new", "create", "version", "versions", "usages" 261: # Allow 262: when "edit", "update" 263: raise Cms::Errors::AccessDenied unless current_user.able_to_edit?(@block) 264: when "destroy", "publish", "revert_to" 265: raise Cms::Errors::AccessDenied unless current_user.able_to_publish?(@block) 266: else 267: raise Cms::Errors::AccessDenied 268: end 269: end
# File app/controllers/cms/content_block_controller.rb, line 107 107: def content_type 108: @content_type ||= ContentType.find_by_key(content_type_name) 109: end
methods that are used to detemine what content block type we are dealing with
# File app/controllers/cms/content_block_controller.rb, line 103 103: def content_type_name 104: self.class.name.sub(/Controller/,'').demodulize.singularize 105: end
# File app/controllers/cms/content_block_controller.rb, line 277 277: def determine_layout 278: 'cms/content_library' 279: 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 235 235: def do_command(result) 236: load_block 237: if yield 238: flash[:notice] = "#{content_type_name.titleize} '#{@block.name}' was #{result}" 239: else 240: flash[:error] = "#{content_type_name.titleize} '#{@block.name}' could not be #{result}" 241: end 242: end
# File app/controllers/cms/content_block_controller.rb, line 135 135: def load_block 136: @block = model_class.find(params[:id]) 137: check_permissions 138: end
# File app/controllers/cms/content_block_controller.rb, line 140 140: def load_block_draft 141: @block = model_class.find(params[:id]) 142: @block = @block.as_of_draft_version if model_class.versioned? 143: check_permissions 144: end
methods for loading one or a collection of blocks
# File app/controllers/cms/content_block_controller.rb, line 121 121: def load_blocks 122: options = {} 123: if params[:section_id] && params[:section_id] != 'all' 124: options[:include] = { :attachment => { :section_node => :section }} 125: options[:conditions] = ["sections.id = ?", params[:section_id]] 126: end 127: options[:page] = params[:page] 128: options[:order] = model_class.default_order if model_class.respond_to?(:default_order) 129: options[:order] = params[:order] unless params[:order].blank? 130: scope = model_class.respond_to?(:list) ? model_class.list : model_class 131: @blocks = scope.searchable? ? scope.search(params[:search]).paginate(options) : scope.paginate(options) 132: check_permissions 133: end
# File app/controllers/cms/content_block_controller.rb, line 111 111: def model_class 112: content_type.model_class 113: end
# File app/controllers/cms/content_block_controller.rb, line 115 115: def model_name 116: model_class.name.underscore.to_sym 117: end
path related methods - available in the view as helpers
# File app/controllers/cms/content_block_controller.rb, line 148 148: def new_block_path(options={}) 149: cms_new_url_for(@block, options) 150: end
# File app/controllers/cms/content_block_controller.rb, line 244 244: def revert_block(to_version) 245: begin 246: @block.revert_to(to_version) 247: rescue Exception => @exception 248: logger.warn "Could not revert #{@block.inspect} to version #{to_version}" 249: logger.warn "#{@exception.message}\n:#{@exception.backtrace.join("\n")}" 250: false 251: end 252: end
# File app/controllers/cms/content_block_controller.rb, line 173 173: def set_default_category 174: if @last_block = model_class.last 175: @block.category = @last_block.category if @block.respond_to?(:category=) 176: end 177: end
methods to setup the view
# File app/controllers/cms/content_block_controller.rb, line 273 273: def set_toolbar_tab 274: @toolbar_tab = :content_library 275: end
# File app/controllers/cms/content_block_controller.rb, line 281 281: def template_directory 282: "cms/blocks" 283: end