Class: CabooseStore::CategoriesController

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

Instance Method Summary collapse

Instance Method Details

#admin_addObject

POST /admin/categories



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/caboose_store/categories_controller.rb', line 21

def admin_add
  return unless user_is_allowed('categories', 'add')
  
  if params[:parent_id].nil? or params[:parent_id].empty?
    render :json => { :error => 'Please select a parent category.' }
  elsif params[:name].nil? or params[:name].empty?
    render :json => { :error => 'This title cannot be empty' }
  else
    category           = Category.new
    category.parent_id = params[:parent_id]
    category.name      = params[:name]
    category.slug      = category.generate_slug
    category.url       = "#{Category.find(params[:parent_id]).url}/#{category.slug}"
    
    if category.save
      render :json => { :success => true, :redirect => "/admin/categories/#{category.id}/edit" }
    else
      render :json => { :error => 'There was an error saving the category.' }
    end
  end
end

#admin_deleteObject

DELETE /admin/categories/:id



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/controllers/caboose_store/categories_controller.rb', line 79

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

#admin_editObject

GET /admin/categories/:id/edit



44
45
46
47
48
# File 'app/controllers/caboose_store/categories_controller.rb', line 44

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

def admin_index
  return unless user_is_allowed('categories', 'view')
  render layout: 'caboose/admin'
end

#admin_newObject

GET /admin/categories/new



15
16
17
18
# File 'app/controllers/caboose_store/categories_controller.rb', line 15

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

#admin_status_optionsObject

GET /admin/products/status-options



95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/caboose_store/categories_controller.rb', line 95

def admin_status_options
  arr = ['Active', 'Inactive', 'Deleted']
  options = []
  arr.each do |status|
    options << {
      :value => status,
      :text => status
    }
  end
  render :json => options
end

#admin_updateObject

PUT /admin/categories/:id



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/caboose_store/categories_controller.rb', line 51

def admin_update
  return unless user_is_allowed('categories', 'edit')
  
  # Define category and initialize response
  category = Category.find(params[:id])
  response = { attributes: Hash.new }
  
  # Iterate over params and update relevant attributes
  params.each do |key, value|
    case key
    when 'name' then category.name = value
    when 'slug' then category.slug = value
    when 'status' then category.status = value
    when 'image' then category.image = value
    end
  end
  
  # Try and save category
  response[:success] = category.save
  
  # If an image is passed, return the url
  response[:attributes][:image] = { value: category.image.url(:medium) } if params[:image]
  
  # Respond to update request
  render :json => response
end