class Humpyard::ElementsController

Humpyard::ElementController is the controller for editing your elements

Public Instance Methods

create() click to toggle source

Create a new element

# File app/controllers/humpyard/elements_controller.rb, line 24
def create
  @element = Humpyard::config.element_types[params[:type]].new params[:element]
        
  unless can? :create, @element.element
    render :json => {
      :status => :failed
    }, :status => 403
    return
  end      
        
  if @element.save
    @prev = Humpyard::Element.find_by_id(params[:prev_id])
    @next = Humpyard::Element.find_by_id(params[:next_id])
    
    do_move(@element, @prev, @next)
  
    insert_options = {
      :element => "hy-id-#{@element.element.id}",
      :url => humpyard_element_path(@element.element),
      :parent => @element.container ? "hy-id-#{@element.container.id}" : "hy-content-#{@element.page_yield_name}"
    }
    
    insert_options[:before] = "hy-id-#{@next.id}" if @next
    insert_options[:after] = "hy-id-#{@prev.id}" if not @next and @prev
  
    render :json => {
      :status => :ok,
      :dialog => :close,
      :insert => [insert_options]
    }
  else
    render :json => {
      :status => :failed, 
      :errors => @element.errors
    }
  end
end
destroy() click to toggle source

Destroy an element

# File app/controllers/humpyard/elements_controller.rb, line 147
def destroy      
  @element = Humpyard::Element.find_by_id(params[:id])
  
  if can? :destroy, @element  
    @element.destroy
  else
    @error = "You have no permission to delete this element (id: #{@element.class}:#{params[:id]})"
    render :error
  end
end
edit() click to toggle source

Dialog content for an existing element

# File app/controllers/humpyard/elements_controller.rb, line 72
def edit
  @element = Humpyard::Element.find(params[:id]).content_data
  
  authorize! :update, @element.element
  
  render :partial => 'edit'
end
inline_edit() click to toggle source

Inline edit content for an existing element

# File app/controllers/humpyard/elements_controller.rb, line 63
def inline_edit
  @element = Humpyard::Element.find(params[:id]).content_data
  
  authorize! :update, @element.element
  
  render :partial => 'inline_edit'
end
move() click to toggle source

Move an element

# File app/controllers/humpyard/elements_controller.rb, line 116
def move
  @element = Humpyard::Element.find(params[:id])
  
  if @element
    unless can? :update, @element
      render :json => {
        :status => :failed
      }, :status => 403
      return
    end
    
    @element.update_attributes(
      :container => Humpyard::Element.find_by_id(params[:container_id]), 
      :page_yield_name => params[:yield_name]
    )
    @prev = Humpyard::Element.find_by_id(params[:prev_id])
    @next = Humpyard::Element.find_by_id(params[:next_id])
    
    do_move(@element, @prev, @next)
    
    render :json => {
      :status => :ok
    }
  else
    render :json => {
      :status => :failed
    }, :status => 404        
  end
end
new() click to toggle source

Dialog content for a new element

# File app/controllers/humpyard/elements_controller.rb, line 7
def new
  @element = Humpyard::config.element_types[params[:type]].new(
    :page_id => params[:page_id], 
    :container_id => params[:container_id].to_i > 0 ? params[:container_id].to_i : nil,
    :page_yield_name => params[:yield_name].blank? ? 'main' : params[:yield_name],
    :shared_state => 0)
  
  authorize! :create, @element.element 
  
  @element_type = params[:type]
  @prev = Humpyard::Element.find_by_id(params[:prev_id])
  @next = Humpyard::Element.find_by_id(params[:next_id])
  
  render :partial => 'edit'
end
show() click to toggle source

Render a given element

# File app/controllers/humpyard/elements_controller.rb, line 159
def show
  @element = Humpyard::Element.find(params[:id])
  
  authorize! :read, @element  
  
  render :partial => 'show', :locals => {:element => @element}
end
update() click to toggle source

Update an existing element

# File app/controllers/humpyard/elements_controller.rb, line 81
def update
  @element = Humpyard::Element.find(params[:id])
  if @element
    unless can? :update, @element
      render :json => {
        :status => :failed
      }, :status => 403
      return
    end
    
    if @element.content_data.update_attributes params[:element]
      render :json => {
        :status => :ok,
        :dialog => :close,
        :replace => [
          { 
            :element => "hy-id-#{@element.id}",
            :url => humpyard_element_path(@element)
          }
        ]
      }
    else
      render :json => {
        :status => :failed, 
        :errors => @element.content_data.errors
      }
    end
  else
    render :json => {
      :status => :failed
    }, :status => 404
  end
end