Class: Caboose::CategoriesController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#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, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in

Instance Method Details

#admin_addObject

POST /admin/categories



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
# File 'app/controllers/caboose/categories_controller.rb', line 26

def admin_add
  return unless user_is_allowed('categories', 'add')
  
  resp = Caboose::StdClass.new
  
  p = Category.where(:id => params[:parent_id]).first
  
  if params[:parent_id].nil? || params[:parent_id].empty? || p.nil?
    resp.error = 'Please select a parent category.'
  elsif params[:name].nil? || params[:name].empty?
    resp.error = 'This title cannot be empty'
  else        
    cat = Category.new(
      :site_id   => @site.id,
      :parent_id => p.id,
      :name      => params[:name],
      :status    => 'Active'
    )
    cat.slug = cat.generate_slug
    cat.url  = "#{p.url}/#{cat.slug}"
    
    if cat.save
      resp.redirect = "/admin/categories/#{cat.id}"
    else
      resp.error = 'There was an error saving the category.'
    end
  end
  
  render :json => resp
end

#admin_deleteObject

DELETE /admin/categories/:id



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/caboose/categories_controller.rb', line 94

def admin_delete
  return unless user_is_allowed('categories', 'delete')
  
  resp = Caboose::StdClass.new
  cat = Category.find(params[:id])
  
  if cat.products.any?
    resp.error = "Can't delete a category that has products in it."
  elsif cat.children.any?
    resp.error = "You can't delete a category that has child categories."
  else
    resp.success = cat.destroy
    resp.redirect = '/admin/categories'
  end
  render :json => resp
end

#admin_editObject

GET /admin/categories/:id/edit



58
59
60
61
62
# File 'app/controllers/caboose/categories_controller.rb', line 58

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

#admin_indexObject

GET /admin/categories



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

def admin_index
  return unless user_is_allowed('categories', 'view')
  
  @top_cat = Caboose::Category.where("site_id = ? and parent_id is null", @site.id).first      
  if @top_cat.nil?
    @top_cat = Caboose::Category.create(:site_id => @site.id, :name => 'All Products', :url => '/')
  end
  render :layout => 'caboose/admin'
end

#admin_newObject

GET /admin/categories/new



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

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

#admin_optionsObject

GET /admin/categories/options



121
122
123
124
125
126
127
128
129
# File 'app/controllers/caboose/categories_controller.rb', line 121

def admin_options      
  @options = []
  cat = Category.where("site_id = ? and parent_id is null", @site.id).first      
  if cat.nil?
    cat = Category.create(:site_id => @site.id, :name => 'All Products', :url => '/')
  end
  admin_options_helper(cat, '')
  render :json => @options
end

#admin_options_helper(cat, prefix) ⇒ Object



131
132
133
134
135
136
# File 'app/controllers/caboose/categories_controller.rb', line 131

def admin_options_helper(cat, prefix)
  @options << { :value => cat.id, :text => "#{prefix}#{cat.name}" }      
  cat.children.each do |c|
    admin_options_helper(c, "#{prefix} - ")
  end      
end

#admin_status_optionsObject

GET /admin/categories/status-options



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

def admin_status_options      
  render :json => [
    { :value => 'Active'   , :text => 'Active'    },
    { :value => 'Inactive' , :text => 'Inactive'  },
    { :value => 'Deleted'  , :text => 'Deleted'   }
  ]
end

#admin_updateObject

PUT /admin/categories/:id



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/controllers/caboose/categories_controller.rb', line 65

def admin_update
  return unless user_is_allowed('categories', 'edit')
  
  # Define category and initialize response
  cat = Category.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
      when 'slug'    then cat.slug   = value
      when 'status'  then cat.status = value
      when 'image'   then cat.image  = value
    end
  end
  
  # Try and save category
  resp.success = cat.save
  
  # If an image is passed, return the url
  resp.attributes[:image] = { :value => cat.image.url(:medium) } if params[:image]
  
  # Respond to update request
  render :json => resp
end