Class: Back::CategoriesController

Inherits:
BackController
  • Object
show all
Defined in:
app/controllers/lato_blog/back/categories_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

This function creates a new category.



39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/lato_blog/back/categories_controller.rb', line 39

def create
  @category = LatoBlog::Category.new(new_category_params)

  if !@category.save
    flash[:danger] = @category.errors.full_messages.to_sentence
    redirect_to lato_blog.new_category_path
    return
  end

  flash[:success] = LANGUAGES[:lato_blog][:flashes][:category_create_success]
  redirect_to lato_blog.category_path(@category.id)
end

#destroyObject

This function destroyes a category.



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/lato_blog/back/categories_controller.rb', line 81

def destroy
  @category = LatoBlog::Category.find_by(id: params[:id])
  return unless check_category_presence

  if !@category.destroy
    flash[:danger] = @category.category_parent.errors.full_messages.to_sentence
    redirect_to lato_blog.edit_category_path(@category.id)
    return
  end
  
  flash[:success] = LANGUAGES[:lato_blog][:flashes][:category_destroy_success]
  redirect_to lato_blog.categories_path(status: 'deleted')
end

#editObject

This function show the view to edit a category.



53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/lato_blog/back/categories_controller.rb', line 53

def edit
  core__set_header_active_page_title(LANGUAGES[:lato_blog][:pages][:categories_edit])
  @category = LatoBlog::Category.find_by(id: params[:id])
  return unless check_category_presence

  if @category.meta_language != cookies[:lato_blog__current_language]
    set_current_language @category.meta_language
  end

  fetch_external_objects
end

#indexObject

This function shows the list of possible categories.



9
10
11
12
13
14
# File 'app/controllers/lato_blog/back/categories_controller.rb', line 9

def index
  core__set_header_active_page_title(LANGUAGES[:lato_blog][:pages][:categories])
  # find categories to show
  @categories = LatoBlog::Category.where(meta_language: cookies[:lato_blog__current_language]).order('title ASC')
  @widget_index_categories = core__widgets_index(@categories, search: 'title', pagination: 10)
end

#newObject

This function shows the view to create a new category.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/lato_blog/back/categories_controller.rb', line 23

def new
  core__set_header_active_page_title(LANGUAGES[:lato_blog][:pages][:categories_new])
  @category = LatoBlog::Category.new

  if params[:language]
    set_current_language params[:language]
  end

  if params[:parent]
    @category_parent = LatoBlog::CategoryParent.find_by(id: params[:parent])
  end

  fetch_external_objects
end

#showObject

This function shows a single category. It create a redirect to the edit path.



17
18
19
20
# File 'app/controllers/lato_blog/back/categories_controller.rb', line 17

def show
  # use edit as default post show page
  redirect_to lato_blog.edit_category_path(params[:id])
end

#updateObject

This function updates a category.



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/lato_blog/back/categories_controller.rb', line 66

def update
  @category = LatoBlog::Category.find_by(id: params[:id])
  return unless check_category_presence

  if !@category.update(edit_category_params)
    flash[:danger] = @category.errors.full_messages.to_sentence
    redirect_to lato_blog.edit_category_path(@category.id)
    return
  end
  
  flash[:success] = LANGUAGES[:lato_blog][:flashes][:category_update_success]
  redirect_to lato_blog.category_path(@category.id)
end