Class: SubscriptionsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/subscriptions_controller.rb

Instance Method Summary (collapse)

Methods inherited from ApplicationController

#allow_cors, #auth!, #check_for_initial_install, #compute_pending_moderation, #current_ability, #delayed_job_running, #gh_latest_version, #latest_version, #precompile_error_catch, #process_notification, #restart_webserver, #set_locale, #set_version, #switch_to_main_app_ability, #switch_to_plugin_ability, #use_plugin_ability, #webserver_supports_restart?

Instance Method Details

- (Object) create

POST /screens/:screen_id/fields/:field_id/subscriptions POST /screens/:screen_id/fields/:field_id/subscriptions.xml



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
# File 'app/controllers/subscriptions_controller.rb', line 70

def create
   @feed_ids = Array.new
	@weights = Array.new
	@errnos = Array.new
	@subscriptions = Array.new
   
	if params.has_key?("subscription_feed")
     @feed_ids = params[:subscription_feed].values
   end
	if params.has_key?("subscription_weight")
		@weights = params[:subscription_weight].values
	end

	@feed_ids.each_with_index do |feed_id, i|
		@subscriptions[i] = Subscription.new
		@subscriptions[i].screen = @screen
		@subscriptions[i].field = @field
		@subscriptions[i].feed_id = feed_id
		@subscriptions[i].weight = @weights[i]
		auth!

		@errnos[i] = !@subscriptions[i].save
     raise "error: #{ @subscriptions}"
	end


   respond_to do |format|
		@errnos.each_with_index do |errno, i|
			if errno
				format.html { render :action => "new" }
				format.xml  { render :xml => @subscriptions[i].errors, :status => :unprocessable_entity }
			end
		end
		format.html { redirect_to(manage_screen_field_subscriptions_path(@screen, @field), :notice => t('subscriptions.records_created')) }
		format.xml  { render :xml => @subscriptions, :status => :created, :location => @subscriptions }
	end
end

- (Object) destroy

DELETE /screens/:screen_id/fields/:field_id/subscriptions/1 DELETE /screens/:screen_id/fields/:field_id/subscriptions/1.xml



181
182
183
184
185
186
187
188
189
190
# File 'app/controllers/subscriptions_controller.rb', line 181

def destroy
  @subscription = Subscription.find(params[:id])
  auth!
  @subscription.destroy

  respond_to do |format|
    format.html { redirect_to(manage_screen_field_subscriptions_url(@screen, @field)) }
    format.xml  { head :ok }
  end
end

- (Object) edit

GET /screens/:screen_id/fields/:field_id/subscriptions/1/edit



63
64
65
66
# File 'app/controllers/subscriptions_controller.rb', line 63

def edit
  @subscription = Subscription.find(params[:id])
  auth!
end

- (Object) get_field



8
9
10
# File 'app/controllers/subscriptions_controller.rb', line 8

def get_field
  @field = Field.find(params[:field_id])
end

- (Object) get_screen



4
5
6
# File 'app/controllers/subscriptions_controller.rb', line 4

def get_screen
  @screen = Screen.find(params[:screen_id])
end

- (Object) index

GET /screens/:screen_id/fields/:field_id/subscriptions GET /screens/:screen_id/fields/:field_id/subscriptions.xml



14
15
16
17
18
19
20
21
22
# File 'app/controllers/subscriptions_controller.rb', line 14

def index
  @subscriptions = @screen.subscriptions.all
  auth!

  respond_to do |format|
    format.html # index.html.erb
    format.xml  { render :xml => @subscriptions }
  end
end

- (Object) manage

GET /screens/:screen_id/fields/:field_id/subscriptions/manage



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/subscriptions_controller.rb', line 25

def manage
  @subscription = Subscription.new 
  #stub out the screen ID so that CanCan can find an owner to auth
  @subscription.screen_id = @screen.id
  auth! 
  
  @this_field = Field.find(params[:field_id])
  @fields = @screen.template.positions.collect{|p| p.field}
  
  respond_to do |format|
    format.html # manage.html.erb
  end
end

- (Object) new

GET /screens/:screen_id/fields/:field_id/subscriptions/new GET /screens/:screen_id/fields/:field_id/subscriptions/new.js



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

def new
  @feed = Feed.find(params[:feed_id])

  respond_to do |format|
    format.html # show.html.erb
    format.js  { render :layout => false }
  end
end

- (Object) save_all

PUT /screens/:screen_id/fields/:field_id/subscriptions/1 PUT /screens/:screen_id/fields/:field_id/subscriptions/1.xml



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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'app/controllers/subscriptions_controller.rb', line 110

def save_all
  #Create parallel arrays to store feeds and their weights for each subscription
  @feed_ids = Array.new
  @weights = Array.new
  @errnos = Array.new
  
  #Populate instance var with all subscription id's submitted
  if params.has_key?("subscription_id")
    @subscription_ids = params[:subscription_id].values
  end
  
  #Correspondingly populate the feed_ids array with the value of subscription 
  if params.has_key?("subscription_feed")
    @feed_ids = params[:subscription_feed].values
  end
  
  #Have the weights of those subsciptions in yet annother corresponding array
  if params.has_key?("subscription_weight")
    @weights = params[:subscription_weight].values
  end
  
  #Do some fun auth stuff before we actually do anything dangerous
  auth!   
  
  #Get a hold of all the subscriptions ID's that aren't in the form and destroy them (as the user deleted them in the form)
  @all_screen_subs = @screen.subscriptions.where(:field_id => @field.id).map(&:id)
  #Use the subtraction operator to get the difference between the arrays
  unless @subscription_ids.nil?
    @subs_to_delete = @all_screen_subs - @subscription_ids.map! { |i| i.to_i }

    unless @subs_to_delete.empty?
      #Search and destroy removed subscriptions
      @subs_to_delete.each do |d|
        Subscription.find(d).destroy
      end
    end    
  end
  
  #raise "feed_ids: #{@feed_ids}"
  if @feed_ids.empty?
    #If the feed_ids array is empty, the user has removed all the subscriptions - NUKE IT FROM ORBIT
    @screen.subscriptions.where(:field_id => @field.id).destroy_all
  else
    #Iterate through all feed ID's the user has submitted (using an iterator i)
    @feed_ids.each_with_index do |feed_id, i|
      #Check for an existing subscription corresponding the the ID the user submitted
      @this_subscription = Subscription.where(:field_id => @field.id, :feed_id => feed_id).first
      if @this_subscription.nil?
        #Create a shiny new object if we don't come up with it
        @this_subscription = Subscription.new
      end
      
      #Update attributes of all subscriptions present in form and populate array with any errors encountered
      @errnos[i] = !@this_subscription.update_attributes(:screen => @screen, :field => @field, :feed_id => feed_id, :weight => @weights[i])
    end
  end

  respond_to do |format|
    @errnos.each_with_index do |errno, i|
      if errno
        format.html { render :action => "new", :notice => "Failed to update subscriptions for this screen position" }
        format.xml  { render :xml => @subscriptions[i].errors, :status => :unprocessable_entity }
      end
    end
    format.html { redirect_to(manage_screen_field_subscriptions_path(@screen, @field), :notice => t('subscriptions.records_updated')) }
    format.xml  { render :xml => @subscriptions, :status => :created, :location => @subscriptions }
  end
end

- (Object) show

GET /screens/:screen_id/fields/:field_id/subscriptions/1 GET /screens/:screen_id/fields/:field_id/subscriptions/1.xml



41
42
43
44
45
46
47
48
49
# File 'app/controllers/subscriptions_controller.rb', line 41

def show
  @subscription = Subscription.find(params[:id])
  auth!

  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @subscription }
  end
end