Class: Humpyard::ElementsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/humpyard/elements_controller.rb

Overview

Humpyard::ElementController is the controller for editing your elements

Instance Method Summary collapse

Instance Method Details

#createObject

Create a new element



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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

#destroyObject

Destroy an element



147
148
149
150
151
152
153
154
155
156
# 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

#editObject

Dialog content for an existing element



72
73
74
75
76
77
78
# 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_editObject

Inline edit content for an existing element



63
64
65
66
67
68
69
# 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

#moveObject

Move an element



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# 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

#newObject

Dialog content for a new element



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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

#showObject

Render a given element



159
160
161
162
163
164
165
# 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

#updateObject

Update an existing element



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 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