Class: HomeController

Inherits:
ApplicationController show all
Includes:
Pagy::Backend
Defined in:
app/controllers/home_controller.rb

Overview

This controller manage the views refering to user

Instance Method Summary collapse

Instance Method Details

#favoriteObject

GET /home/favorite

render favorite

Returns:

  • (Object)

    render home/favorite



83
# File 'app/controllers/home_controller.rb', line 83

def favorite; end

#favorite_createObject

POST /faq/1 or /faq/1.json



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

def favorite_create
  if UserFaq.create(user_id: current_user.id, faq_id: @faq.id)
    flash.now[:success] = 'Faq aggiunta tra i preferiti!'
    get_user_faqs
  else
    flash.now[:success] = 'Si è verificato un errore durante la creazione della Faq nei preferiti!'
  end
  render turbo_stream: [
    turbo_stream.replace(:flashes, partial: "flashes"),
    turbo_stream.replace("faq_#{@faq.id}", partial: "home/faq", locals: {faq: @faq, user_faq_ids: @user_faq_ids})
  ]
end

#favorite_destroyObject

DELETE /faq/1 or /faq/1.json



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/controllers/home_controller.rb', line 112

def favorite_destroy
  user_faq = UserFaq.find_by(user_id: current_user.id, faq_id: @faq.id)
  return if user_faq.blank?

  if user_faq.destroy
    flash.now[:success] = 'Faq rimossa dai preferiti!'
    get_user_faqs
  else
    flash.now[:error] = 'Si è verificato un errore durante la cancellazione della Faq dai preferiti!'
  end
  render turbo_stream: [
    turbo_stream.replace(:flashes, partial: "flashes"),
    turbo_stream.replace("faq_#{@faq.id}", partial: "home/faq", locals: {faq: @faq, user_faq_ids: @user_faq_ids})
  ]
end

#favorite_listObject

GET /home/favorite_list

render favorite_list

Returns:

  • (Object)

    render home/favorite_list



89
90
91
92
93
94
95
# File 'app/controllers/home_controller.rb', line 89

def favorite_list
  @filters = filter_params
  @page = params[:page] || 1
  @faqs = @user.faqs.accessible_by(current_ability).actived.approved
  @faqs = @faqs.where("title ilike '%?%'", @filters[:text]) if @filters[:text].present?
  @pagy, @faqs = pagy(@faqs, page: @page, link_extra: "data-turbo-frame='faqs'")
end

#indexObject

GET /home/index

render faqs

Returns:

  • (Object)

    render home/index



16
17
18
19
20
21
# File 'app/controllers/home_controller.rb', line 16

def index
  @categories = ActsAsTaggableOn::Tag.most_used(Settings.faq.categories_on_top)
  faqs = Faq.accessible_by(current_ability).actived.approved
  @faqs = faqs.tagged_with(@categories, any: true)
  @sample = faqs.find(faqs.pluck(:id).sample)
end

#listObject

GET /home/list

render user faqs

Returns:

  • (Object)

    render home/faqs



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/home_controller.rb', line 27

def list
  @filters = filter_params
  @page = params[:page] || 1
  faqs = Faq.accessible_by(current_ability).actived.approved
  @evidences = faqs.evidenced.limit(Settings.faq.max_faqs_in_evidence)
  @tops = faqs.approved.where('counter > 0').on_top(Settings.faq.most_requested)
  if @filters[:text].present? || @filters[:category].present?
    faqs = faqs.search_by_title(@filters[:text].gsub(/[^\w\s]+/, ' ').remove(/[^\w\s]+/)) if @filters[:text].present?
    faqs = faqs.tagged_with(@filters[:category].remove(/[^\w\s]+/), any: true) if @filters[:category].present?
    @faqs = faqs
  end
  flash.now[:success] = 'Caricamento completato'
end

#proposeObject

GET /home/propose

render propose

Returns:

  • (Object)

    render home/propose



77
# File 'app/controllers/home_controller.rb', line 77

def propose; end

#propose_sendObject

POST /home/propose

render propose_send

Returns:

  • (Object)

    render home/propose_send



132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/controllers/home_controller.rb', line 132

def propose_send
  @author = propose_params[:author_id].present? ? User.find(propose_params[:author_id]) : ''
  if @author.present? && propose_params[:title].present?
    UserMailer.with(author: @author, propose: propose_params).propose_email.deliver_now
    User.admins.map { |u| FaqMailer.with(user: u, author: @author, propose: propose_params).propose_email.deliver_now }
    flash.now[:success] = 'Invio proposta completato'
  else
    flash.now[:error] = 'Non è stato possibile inviare la richiesta' if @author.blank?
    flash.now[:error] = 'Il titolo è obbligatorio!' if propose_params[:title].blank?
  end
  render turbo_stream: turbo_stream.replace(:flashes, partial: "flashes")
end

#searchObject

GET /:text

render index

Returns:

  • (Object)

    render home/index



53
54
55
56
57
58
# File 'app/controllers/home_controller.rb', line 53

def search
  @filters = filter_params
  @filters[:text] = params[:text].gsub(/[^\w\s]+/, ' ').remove(/[^\w\s]+/) if params[:text].present?
  flash.now[:success] = 'Caricamento completato'
  render :index
end

#search_by_titleObject

GET /faq/:find_by_title

render show

Returns:

  • (Object)

    render home/show



64
65
66
67
68
69
70
71
# File 'app/controllers/home_controller.rb', line 64

def search_by_title
  @filters = filter_params
  @filters[:text] = params[:text].gsub(/[^\w\s]+/, ' ').remove(/[^\w\s]+/) if params[:text].present?
  faqs = Faq.accessible_by(current_ability)
  @faq = faqs.find_by("LOWER(title) = ?", @filters[:text].gsub(/[^\w\s]+/, ' ').remove(/[^\w\s]+/).downcase) if @filters[:text].present?
  FaqCounterJob.perform_later(faq: @faq)
  render :show
end

#showObject

GET /home/1/show

render faq

Returns:

  • (Object)

    render home/show



45
46
47
# File 'app/controllers/home_controller.rb', line 45

def show
  FaqCounterJob.perform_later(faq: @faq)
end