Class: EventsController

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

Instance Method Summary collapse

Instance Method Details

#createObject

POST /events POST /events.json



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/controllers/events_controller.rb', line 104

def create
  @event = Event.new(event_params)
  @event.set_date

  respond_to do |format|
    if @event.save
      flash[:notice] = t('controller.successfully_created', model: t('activerecord.models.event'))
      format.html { redirect_to(@event) }
      format.json { render json: @event, status: :created, location: @event }
    else
      prepare_options
      format.html { render action: "new" }
      format.json { render json: @event.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject

DELETE /events/1 DELETE /events/1.json



143
144
145
146
147
148
149
150
# File 'app/controllers/events_controller.rb', line 143

def destroy
  @event.destroy

  respond_to do |format|
    format.html { redirect_to events_url }
    format.json { head :no_content }
  end
end

#editObject

GET /events/1/edit



97
98
99
100
# File 'app/controllers/events_controller.rb', line 97

def edit
  @event = Event.find(params[:id])
  prepare_options
end

#indexObject

GET /events GET /events.json



12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
56
57
58
59
# File 'app/controllers/events_controller.rb', line 12

def index
  @count = {}
  query = params[:query].to_s.strip
  @query = query.dup
  query = query.gsub(' ', ' ')
  tag = params[:tag].to_s if params[:tag].present?
  date = params[:date].to_s if params[:date].present?
  per_page = ( params[:format] == "json" ) ? 500 : Event.default_per_page
  mode = params[:mode]

  search = Sunspot.new_search(Event)
  library = @library
  search.build do
    fulltext query if query.present?
    with(:library_id).equal_to library.id if library
    # with(:tag).equal_to tag
    if date
      with(:start_at).less_than_or_equal_to Time.zone.parse(date)
      with(:end_at).greater_than Time.zone.parse(date)
    end
    if params[:start].present? and params[:end].present?
      with(:start_at).greater_than_or_equal_to Time.zone.parse(params[:start])
      with(:end_at).less_than_or_equal_to Time.zone.parse(params[:end]).end_of_day
    end
    case mode
    when 'upcoming'
      with(:start_at).greater_than Time.zone.now.beginning_of_day
    when 'past'
      with(:start_at).less_than Time.zone.now.beginning_of_day
    end
    order_by(:start_at, :desc)
  end

  page = params[:page] || 1
  search.query.paginate(page.to_i, per_page)
  @events = search.execute!.results
  @count[:query_result] = @events.total_entries

  respond_to do |format|
    format.html # index.html.erb
    format.html.phone
    format.json
    format.rss  { render layout: false }
    format.text
    format.atom
    format.ics
  end
end

#newObject

GET /events/new GET /events/new.json



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/events_controller.rb', line 75

def new
   prepare_options
   if params[:date]
     begin
       date = Time.zone.parse(params[:date])
     rescue ArgumentError
       date = Time.zone.now.beginning_of_day
       flash[:notice] = t('page.invalid_date')
     end
   else
     date = Time.zone.now.beginning_of_day
   end
   @event = Event.new(start_at: date, end_at: date)
   @event.library = @library

   respond_to do |format|
     format.html # new.html.erb
     format.json { render json: @event }
   end
end

#showObject

GET /events/1 GET /events/1.json



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

def show
  @event = Event.find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.html.phone
    format.json { render json: @event }
  end
end

#updateObject

PUT /events/1 PUT /events/1.json



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/controllers/events_controller.rb', line 123

def update
  @event = Event.find(params[:id])
  @event.set_date

  respond_to do |format|
    if @event.update(event_params)

      flash[:notice] = t('controller.successfully_updated', model: t('activerecord.models.event'))
      format.html { redirect_to(@event) }
      format.json { head :no_content }
    else
      prepare_options
      format.html { render action: "edit" }
      format.json { render json: @event.errors, status: :unprocessable_entity }
    end
  end
end