Class: Editor::FactsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/editor/facts_controller.rb

Overview

This controller manage Fact model for editors

Instance Method Summary collapse

Instance Method Details

#createObject

POST /editor/facts


30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/editor/facts_controller.rb', line 30

def create
  @fact = Fact.new(fact_params)
  if @fact.save
    @status = { success: 'Evento creato' }
    redirect_to editor_fact_path(@fact)
  else
    @status = { error: 'ERRORE - Evento non creato. Verifica gli errori.' }
    render action: :new
  end
end

#destroyObject

DELETE /editor/facts/:id


53
54
55
56
57
58
59
60
61
# File 'app/controllers/editor/facts_controller.rb', line 53

def destroy
  if @fact.destroy
    @status = { success: 'Evento eliminato' }
    redirect_to editor_facts_path
  else
    @status = { error: 'ERRORE - Evento non eliminato. Verifica gli errori.' }
    render action: :edit
  end
end

#editObject

GET /editor/facts/:id/edit


27
# File 'app/controllers/editor/facts_controller.rb', line 27

def edit; end

#fact_paramsObject (private)

Filter params for set a Fact


81
82
83
# File 'app/controllers/editor/facts_controller.rb', line 81

def fact_params
  params.require(:fact).permit(:tickets_frequency, :title, :group_id, :body, :start_on, :stop_on, :pinned, :image, :where)
end

#filter_paramsObject (private)

Filter params for search a Fact


76
77
78
# File 'app/controllers/editor/facts_controller.rb', line 76

def filter_params
  params.fetch(:filter, {}).permit(:text, :type)
end

#indexObject

GET /editor/facts


9
# File 'app/controllers/editor/facts_controller.rb', line 9

def index; end

#listObject

GET /editor/facts/list


12
13
14
15
16
# File 'app/controllers/editor/facts_controller.rb', line 12

def list
  type = filter_params[:type] == 'history' ? 'history' : 'future'
  @text = ['facts.title ilike :text', { text: "%#{filter_params[:text]}%" }] if filter_params[:text].present?
  @pagy, @facts = pagy(current_user.facts.send(type).where(@text))
end

#newObject

GET /editor/facts/new


22
23
24
# File 'app/controllers/editor/facts_controller.rb', line 22

def new
  @fact = Fact.new
end

#set_factObject (private)

set @fact for current user when needed


66
67
68
# File 'app/controllers/editor/facts_controller.rb', line 66

def set_fact
  @fact = current_user.facts.find(params[:id])
end

#set_groupsObject (private)

set a list of Group


71
72
73
# File 'app/controllers/editor/facts_controller.rb', line 71

def set_groups
  @groups = Group.pluck :title, :id
end

#showObject

GET /editor/facts/:id


19
# File 'app/controllers/editor/facts_controller.rb', line 19

def show; end

#updateObject

PATCH/PUT /editor/facts/:id


42
43
44
45
46
47
48
49
50
# File 'app/controllers/editor/facts_controller.rb', line 42

def update
  if @fact.update(fact_params)
    @status = { success: 'Evento Aggiornato' }
    render action: :show
  else
    @status = { error: 'ERRORE - Evento non aggiornato. Verifica gli errori.' }
    render :edit
  end
end