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
24 def create
25   @element = Humpyard::config.element_types[params[:type]].new params[:element]
26         
27   unless can? :create, @element.element
28     render :json => {
29       :status => :failed
30     }, :status => 403
31     return
32   end      
33         
34   if @element.save
35     @prev = Humpyard::Element.find_by_id(params[:prev_id])
36     @next = Humpyard::Element.find_by_id(params[:next_id])
37     
38     do_move(@element, @prev, @next)
39   
40     insert_options = {
41       :element => "hy-id-#{@element.element.id}",
42       :url => humpyard_element_path(@element.element),
43       :parent => @element.container ? "hy-id-#{@element.container.id}" : "hy-content-#{@element.page_yield_name}"
44     }
45     
46     insert_options[:before] = "hy-id-#{@next.id}" if @next
47     insert_options[:after] = "hy-id-#{@prev.id}" if not @next and @prev
48   
49     render :json => {
50       :status => :ok,
51       :dialog => :close,
52       :insert => [insert_options]
53     }
54   else
55     render :json => {
56       :status => :failed, 
57       :errors => @element.errors
58     }
59   end
60 end
destroy() click to toggle source

Destroy an element

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

Dialog content for an existing element

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

Inline edit content for an existing element

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

Move an element

    # File app/controllers/humpyard/elements_controller.rb
116 def move
117   @element = Humpyard::Element.find(params[:id])
118   
119   if @element
120     unless can? :update, @element
121       render :json => {
122         :status => :failed
123       }, :status => 403
124       return
125     end
126     
127     @element.update_attributes(
128       :container => Humpyard::Element.find_by_id(params[:container_id]), 
129       :page_yield_name => params[:yield_name]
130     )
131     @prev = Humpyard::Element.find_by_id(params[:prev_id])
132     @next = Humpyard::Element.find_by_id(params[:next_id])
133     
134     do_move(@element, @prev, @next)
135     
136     render :json => {
137       :status => :ok
138     }
139   else
140     render :json => {
141       :status => :failed
142     }, :status => 404        
143   end
144 end
new() click to toggle source

Dialog content for a new element

   # File app/controllers/humpyard/elements_controller.rb
 7 def new
 8   @element = Humpyard::config.element_types[params[:type]].new(
 9     :page_id => params[:page_id], 
10     :container_id => params[:container_id].to_i > 0 ? params[:container_id].to_i : nil,
11     :page_yield_name => params[:yield_name].blank? ? 'main' : params[:yield_name],
12     :shared_state => 0)
13   
14   authorize! :create, @element.element 
15   
16   @element_type = params[:type]
17   @prev = Humpyard::Element.find_by_id(params[:prev_id])
18   @next = Humpyard::Element.find_by_id(params[:next_id])
19   
20   render :partial => 'edit'
21 end
show() click to toggle source

Render a given element

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

Update an existing element

    # File app/controllers/humpyard/elements_controller.rb
 81 def update
 82   @element = Humpyard::Element.find(params[:id])
 83   if @element
 84     unless can? :update, @element
 85       render :json => {
 86         :status => :failed
 87       }, :status => 403
 88       return
 89     end
 90     
 91     if @element.content_data.update_attributes params[:element]
 92       render :json => {
 93         :status => :ok,
 94         :dialog => :close,
 95         :replace => [
 96           { 
 97             :element => "hy-id-#{@element.id}",
 98             :url => humpyard_element_path(@element)
 99           }
100         ]
101       }
102     else
103       render :json => {
104         :status => :failed, 
105         :errors => @element.content_data.errors
106       }
107     end
108   else
109     render :json => {
110       :status => :failed
111     }, :status => 404
112   end
113 end