-
Notifications
You must be signed in to change notification settings - Fork 82
/
events_controller.rb
135 lines (117 loc) · 3.83 KB
/
events_controller.rb
1
2
3
4
5
6
7
8
9
10
11
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
class EventsController < ApplicationController
acceptable_includes :line_items, :user, :tagged_items
before_filter :find_container, :find_events, :only => :index
before_filter :find_subscription, :only => %w(new create)
before_filter :find_event, :except => %w(index new create)
def index
respond_to do |format|
format.js do
json = events.to_json(eager_options(:root => "events", :include => { :tagged_items => { :only => [:amount, :id], :methods => :name }, :line_items => { :only => [:account_id, :bucket_id, :amount, :role], :methods => [] }}))
render :update do |page|
page << "Events.doneLoadingRecalledEvents(#{json})"
end
end
format.xml do
render :xml => events.to_xml(eager_options(:root => "events"))
end
end
end
def show
respond_to do |format|
format.js
format.xml { render :xml => event.to_xml(eager_options) }
end
end
def edit
end
def new
@event = subscription.events.prepare(params)
respond_to do |format|
format.html
format.xml { render :xml => event.to_xml(:include => [:line_items, :tagged_items]) }
end
end
def create
@event = subscription.events.create!(params[:event], :user => user)
respond_to do |format|
format.js
format.xml do
render :status => :created, :location => event_url(@event),
:xml => @event.to_xml(:include => [:line_items, :tagged_items])
end
end
rescue ActiveRecord::RecordInvalid => error
@event = error.record
respond_to do |format|
format.js
format.xml { render :status => :unprocessable_entity, :xml => @event.errors }
end
end
def update
event.update_attributes!(params[:event])
respond_to do |format|
format.js
format.xml { render :xml => event.to_xml(:include => [:line_items, :tagged_items]) }
end
rescue ActiveRecord::RecordInvalid => error
respond_to do |format|
format.js
format.xml { render :status => :unprocessable_entity, :xml => event.errors }
end
end
def destroy
event.destroy
respond_to do |format|
format.js
format.xml { head :ok }
end
end
protected
attr_reader :event, :container, :account, :bucket, :tag, :events
helper_method :event, :container, :account, :bucket
def find_event
@event = Event.find(params[:id])
@subscription = user.subscriptions.find(@event.subscription_id)
end
def find_container
if params[:subscription_id]
@container = find_subscription
elsif params[:account_id]
@container = @account = Account.find(params[:account_id])
@subscription = user.subscriptions.find(@account.subscription_id)
elsif params[:bucket_id]
@container = @bucket = Bucket.find(params[:bucket_id])
@subscription = user.subscriptions.find(@bucket.account.subscription_id)
elsif params[:tag_id]
@container = @tag = Tag.find(params[:tag_id])
@subscription = user.subscriptions.find(@tag.subscription_id)
else
raise ArgumentError, "no container specified for event listing"
end
end
def find_events
method = :page
case container
when Subscription then
association = :events
method = :recent
when Account
association = :account_items
when Bucket
association = :line_items
when Tag
association = :tagged_items
else
raise ArgumentError, "unsupported container type #{container.class}"
end
more_pages, list = container.send(association).send(method, params[:page], :size => params[:size], :actor => params[:actor])
unless list.first.is_a?(Event)
list = list.map do |item|
event = item.event
event.amount = item.amount
event
end
end
@events = list
end
end