Class: Caboose::PostsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/caboose/posts_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#add_ga_event, #admin_bulk_add, #admin_bulk_delete, #admin_bulk_update, #admin_edit, #admin_json, #before_action, #before_before_action, #hashify_query_string, #init_cart, #logged_in?, #logged_in_user, #login_user, #logout_user, #parse_url_params, #reject_param, #under_construction_or_forwarding_domain?, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in

Instance Method Details

#admin_addObject

POST /admin/posts



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'app/controllers/caboose/posts_controller.rb', line 179

def admin_add
  return if !user_is_allowed('posts', 'add')
  
  resp = Caboose::StdClass.new({
    'error' => nil,
    'redirect' => nil
  })

  post = Post.new
  post.site_id = @site.id
  post.title = params[:title]                  
  post.published = false
  
  if post.title == nil || post.title.length == 0
    resp.error = 'A title is required.'      
  else
    post.save        
    post.set_slug_and_uri(post.title)                
    resp.redirect = "/admin/posts/#{post.id}/edit"
  end
  
  render :json => resp
end

#admin_add_to_categoryObject

PUT /admin/posts/:id/add-to-category



204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/controllers/caboose/posts_controller.rb', line 204

def admin_add_to_category
  return if !user_is_allowed('posts', 'edit')
  
  post_id = params[:id]
  cat_id = params[:post_category_id]
  
  if !PostCategoryMembership.exists?(:post_id => post_id, :post_category_id => cat_id)
    PostCategoryMembership.create(:post_id => post_id, :post_category_id => cat_id)
  end
  
  render :json => true      
end

#admin_deleteObject

DELETE /admin/posts/:id



239
240
241
242
243
244
245
246
247
# File 'app/controllers/caboose/posts_controller.rb', line 239

def admin_delete
  return if !user_is_allowed('posts', 'edit')
  
  post_id = params[:id]
  PostCategoryMembership.where(:post_id => post_id).destroy_all
  Post.where(:id => post_id).destroy_all
  
  render :json => { 'redirect' => '/admin/posts' }      
end

#admin_delete_formObject

GET /admin/posts/:id/delete



232
233
234
235
236
# File 'app/controllers/caboose/posts_controller.rb', line 232

def admin_delete_form
  return if !user_is_allowed('posts', 'delete')
  @post = Post.find(params[:id])
  render :layout => 'caboose/admin'
end

#admin_edit_categoriesObject

GET /admin/posts/:id/categories



100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/caboose/posts_controller.rb', line 100

def admin_edit_categories
  return if !user_is_allowed('posts', 'edit')    
  @post = Post.find(params[:id])
  @categories = PostCategory.where(:site_id => @site.id).reorder(:name).all
  if @categories.nil? || @categories.count == 0
    PostCategory.create(:site_id => @site.id, :name => 'General News')
    @categories = PostCategory.where(:site_id => @site.id).reorder(:name).all
  end
  render :layout => 'caboose/admin'
end

#admin_edit_contentObject

GET /admin/posts/:id/content



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/caboose/posts_controller.rb', line 84

def admin_edit_content
  return if !user_is_allowed('posts', 'edit')    
  @post = Post.find(params[:id])
  if @post.body
    @post.preview = @post.body
    @post.body = nil
    @post.save
  end
  if @post.block.nil?      
    redirect_to "/admin/posts/#{@post.id}/layout"
    return
  end
  @editing = true
end

#admin_edit_generalObject

GET /admin/posts/:id/edit



69
70
71
72
73
74
# File 'app/controllers/caboose/posts_controller.rb', line 69

def admin_edit_general
  return if !user_is_allowed('posts', 'edit')    
  @post = Post.find(params[:id])
  @post.verify_custom_field_values_exist
  render :layout => 'caboose/admin'
end

#admin_edit_layoutObject

GET /admin/posts/:id/layout



112
113
114
115
116
# File 'app/controllers/caboose/posts_controller.rb', line 112

def admin_edit_layout
  return unless user_is_allowed('posts', 'edit')      
  @post = Post.find(params[:id])
  render :layout => 'caboose/admin'
end

#admin_edit_previewObject

GET /admin/posts/:id/preview



77
78
79
80
81
# File 'app/controllers/caboose/posts_controller.rb', line 77

def admin_edit_preview
  return if !user_is_allowed('posts', 'edit')    
  @post = Post.find(params[:id])      
  render :layout => 'caboose/admin'
end

#admin_indexObject

GET /admin/posts



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/controllers/caboose/posts_controller.rb', line 45

def admin_index
  return if !user_is_allowed('posts', 'view')
    
  @gen = Caboose::PageBarGenerator.new(params, {
      'site_id'     => @site.id,
      'name'        => ''
  },{
      'model'       => 'Caboose::Post',
      'sort'        => 'created_at DESC',
      'desc'        => false,
      'base_url'    => '/admin/posts'
  })
  @posts = @gen.items    
  render :layout => 'caboose/admin'
end

#admin_json_singleObject

GET /admin/posts/:id/json



62
63
64
65
66
# File 'app/controllers/caboose/posts_controller.rb', line 62

def admin_json_single
  return if !user_is_allowed('posts', 'edit')    
  @post = Post.find(params[:id])
  render :json => @post
end

#admin_newObject

GET /admin/posts/new



172
173
174
175
176
# File 'app/controllers/caboose/posts_controller.rb', line 172

def admin_new
  return if !user_is_allowed('posts', 'new')  
  @new_post = Post.new  
  render :layout => 'caboose/admin'
end

#admin_remove_from_categoryObject

PUT /admin/posts/:id/remove-from-category



218
219
220
221
222
223
224
225
226
227
228
229
# File 'app/controllers/caboose/posts_controller.rb', line 218

def admin_remove_from_category
  return if !user_is_allowed('posts', 'edit')
  
  post_id = params[:id]
  cat_id = params[:post_category_id]
  
  if PostCategoryMembership.exists?(:post_id => post_id, :post_category_id => cat_id)
    PostCategoryMembership.where(:post_id => post_id, :post_category_id => cat_id).destroy_all
  end
  
  render :json => true      
end

#admin_updateObject

POST /admin/posts/:id



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/controllers/caboose/posts_controller.rb', line 131

def admin_update      
  return if !user_is_allowed('posts', 'edit')
  
  resp = Caboose::StdClass.new({'attributes' => {}})
  post = Post.find(params[:id])
  
  save = true
  params.each do |name, value|    
    case name                      
      when 'site_id'    then post.site_id    = value.to_i
      when 'slug'       then post.set_slug_and_uri(value)                        
      when 'title'      then post.title      = value            
      when 'subtitle'   then post.subtitle   = value
      when 'author'     then post.author     = value
      when 'body'       then post.body       = value
      when 'preview'    then post.preview    = value
      when 'hide'       then post.hide       = value
      when 'image_url'  then post.image_url  = value
      when 'published'  then post.published  = value
      when 'created_at' then post.created_at = DateTime.strptime(value,'%m/%d/%Y')
      when 'updated_at' then post.updated_at = DateTime.parse(value)
    end
  end
  resp.success = save && post.save      
  render :json => resp
end

#admin_update_imageObject

POST /admin/posts/:id/image



159
160
161
162
163
164
165
166
167
168
169
# File 'app/controllers/caboose/posts_controller.rb', line 159

def admin_update_image
  return if !user_is_allowed('posts', 'edit')
  
  resp = Caboose::StdClass.new
  post = Post.find(params[:id])
  post.image = params[:image]            
  resp.success = post.save
  resp.attributes = { 'image' => { 'value' => post.image.url(:thumb) }}
  
  render :text => resp.to_json
end

#admin_update_layoutObject

PUT /admin/posts/:id/layout



119
120
121
122
123
124
125
126
127
128
# File 'app/controllers/caboose/posts_controller.rb', line 119

def admin_update_layout
  return unless user_is_allowed('posts', 'edit')      
  bt = BlockType.find(params[:block_type_id])
  Block.where(:post_id => params[:id]).destroy_all
  Block.create(:post_id => params[:id], :block_type_id => params[:block_type_id], :name => bt.name)
  resp = Caboose::StdClass.new({
    'redirect' => "/admin/posts/#{params[:id]}/content"
  })
  render :json => resp
end

#indexObject

GET /posts



7
8
9
# File 'app/controllers/caboose/posts_controller.rb', line 7

def index
	@posts = Post.where(:published => true).limit(5).order('created_at DESC')
end

#showObject

GET /posts/:id GET /posts/:year/:month/:day/:slug



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/caboose/posts_controller.rb', line 22

def show
  
  # Make sure we're not under construction or on a forwarded domain
  return if under_construction_or_forwarding_domain?
  
  if params[:id]
    @post = Post.where(:id => params[:id]).first
  else      
    # Find the page with an exact URI match        
    uri = "#{params[:year]}/#{params[:month]}/#{params[:day]}/#{params[:slug]}"
    @post = Post.where(:site_id => @site.id, :uri => request.fullpath).first
  end      
  render :file => "caboose/extras/error404" and return if @post.nil?                 

  @post = Caboose.plugin_hook('post_content', @post)
  @editmode = !params['edit'].nil? && user.is_allowed('posts', 'edit') ? true : false  
end