Class: Caboose::PostCategoriesController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#add_ga_event, #admin_bulk_add, #admin_bulk_delete, #admin_bulk_update, #admin_json, #admin_json_single, #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/post-categories



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/caboose/post_categories_controller.rb', line 26

def admin_add
  return unless user_is_allowed('post_categories', 'add')
  
  resp = Caboose::StdClass.new

  if params[:name].nil? || params[:name].empty?
    resp.error = 'The category name cannot be empty.'
  else        
    cat = PostCategory.new(
      :site_id   => @site.id,
      :name      => params[:name]
    )
    
    if cat.save
      resp.redirect = "/admin/post-categories/#{cat.id}"
    else
      resp.error = 'There was an error saving the category.'
    end
  end
  
  render :json => resp
end

#admin_deleteObject

DELETE /admin/post-categories/:id



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/caboose/post_categories_controller.rb', line 80

def admin_delete
  return unless user_is_allowed('post_categories', 'delete')
  
  resp = Caboose::StdClass.new
  cat = PostCategory.find(params[:id])
  
  if cat.posts.any?
    resp.error = "Can't delete a post category that has posts in it."
  else
    resp.success = cat.destroy
    resp.redirect = '/admin/post-categories'
  end
  render :json => resp
end

#admin_editObject

GET /admin/post-categories/:id



50
51
52
53
54
# File 'app/controllers/caboose/post_categories_controller.rb', line 50

def admin_edit
  return unless user_is_allowed('post_categories', 'edit')    
  @category = PostCategory.find(params[:id])
  render :layout => 'caboose/admin'
end

#admin_indexObject

GET /admin/post-categories



9
10
11
12
13
14
15
16
17
# File 'app/controllers/caboose/post_categories_controller.rb', line 9

def admin_index
  return unless user_is_allowed('post_categories', 'view')
  
  @main_cat = PostCategory.where("site_id = ?", @site.id).first      
  if @main_cat.nil?
    @main_cat = PostCategory.create(:site_id => @site.id, :name => 'General News')
  end
  render :layout => 'caboose/admin'
end

#admin_newObject

GET /admin/post-categories/new



20
21
22
23
# File 'app/controllers/caboose/post_categories_controller.rb', line 20

def admin_new
  return unless user_is_allowed('post_categories', 'add')
  render :layout => 'caboose/admin'
end

#admin_updateObject

PUT /admin/post-categories/:id



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/caboose/post_categories_controller.rb', line 57

def admin_update
  return unless user_is_allowed('post_categories', 'edit')
  
  # Define category and initialize response
  cat = PostCategory.find(params[:id])
  resp = Caboose::StdClass.new({ :attributes => {} })
  
  # Iterate over params and update relevant attributes
  params.each do |key, value|
    case key
      when 'site_id' then cat.name   = value
      when 'name'    then cat.name   = value
    end
  end
  
  # Try and save category
  resp.success = cat.save
  
  # Respond to update request
  render :json => resp
end