Class: ContentsController

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

Instance Method Summary (collapse)

Methods inherited from ApplicationController

#after_sign_in_path_for, #allow_cors, #allow_screen_if_unsecured, #apply_relative_root, #auth!, #check_for_initial_install, #compute_pending_moderation, #current_ability, #current_accessor, #current_screen, #http_basic_user_name_and_password, #precompile_error_catch, #process_notification, #restart_webserver, #screen_api, #set_locale, #set_time_zone, #set_version, #sign_in_screen, #sign_out_screen, #switch_to_main_app_ability, #switch_to_plugin_ability, #use_plugin_ability, #webserver_supports_restart?

Instance Method Details

- (Object) act

PUT /contents/1/act Trigger custom actions for the content.



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'app/controllers/contents_controller.rb', line 252

def act
  @content = Content.find(params[:id])
  auth!(:action => :read)
  action_name = params[:action_name].to_sym
  params[:current_user] = current_user
  result = @content.perform_action(action_name, params)

  respond_to do |format|
    format.html do
      # reload to get the updated information
      @content = Content.find(params[:id])
      @user = User.find(@content.user_id)

      flash.now[:notice] = (result.nil? ? 'Unable to perform action' : result)
      render :show
    end
    format.js do
      if result.nil?
        render :text => 'Unable to perform action.', :status => 400
      else
        render :text => result, :status => 200
      end
    end
  end
end

- (Object) add_feed



76
77
78
79
80
81
82
83
# File 'app/controllers/contents_controller.rb', line 76

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

  respond_to do |format|
    format.js
  end
end

- (Object) create

POST /contents POST /contents.xml



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

def create
  prams = content_params
  if prams.include?("media_attributes") && prams[:media_attributes]["0"].include?("id")
    # pull out the media_id otherwise, new will try to find it even though it's not yet linked
    media_id = prams[:media_attributes]["0"][:id]
    prams[:media_attributes]["0"].delete :id
  end
  # some content, like the ticker_text, can have a kind other than it's model's default
  if prams.include?("kind_id")
    kind = Kind.find(prams[:kind_id])
    prams.delete :kind_id
  end
  @content = @content_const.new(prams)
  @content.kind = kind if !kind.nil?
  @content.user = current_user
  auth!

  @feed_ids = feed_ids

  remove_empty_media_param
  if !media_id.blank?
    # if the media_id was passed in then there is an existing media
    # record that needs to be attached to this content
    @media = Media.find(media_id)
    # only reassign if not already assigned
    if @media[:key] == 'preview' && @media[:attachable_id] == 0
      @media[:key] = 'original'
      @content.media.clear
      @content.media.concat(@media)
    end
  end
  respond_to do |format|
    # remove the media entry added in the _form_top partial if it is completely empty
    @content.media.reject! { |m| m.file_name.nil? && m.file_type.nil? && m.file_size.nil? && m.file_data.nil? }
    begin
      results = @content.save
    rescue Concerto::ContentConverter::Unconvertable => e
      results = false
      flash.now[:error] = e.message
    rescue StandardError => e
       results = false
       flash.now[:error] = e.message
    end
    if results
      process_notification(@content, {}, process_notification_options({
        :params => {
          :content_name => @content.name,
          :content_type => @content.class.model_name.human
        },
        :key => "content.#{action_name}"}))
      # Copy over the duration to each submission instance
      create_submissions
      @content.save #This second save adds the submissions
      if @feed_ids == []
        format.html { redirect_to(@content, :notice => t(:content_created_no_feeds)) }
        format.xml { render :xml => @content, :status => :created, :location => @content }
      else
        format.html { redirect_to(@content, :notice => t(:content_created)) }
        format.xml { render :xml => @content, :status => :created, :location => @content }
      end
    else
      @feeds = submittable_feeds
      format.html { render :action => "new" }
      format.xml { render :xml => @content.errors, :status => :unprocessable_entity }
    end
  end
end

- (Object) destroy

DELETE /contents/1 DELETE /contents/1.xml



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'app/controllers/contents_controller.rb', line 201

def destroy
  @content = Content.find(params[:id])
  auth!

  process_notification(@content, {}, process_notification_options({
    :params => {
      :content_name => @content.name,
      :content_type => @content.class.model_name.human
     },
    :key => "content.#{action_name}"}))
  @content.destroy

  respond_to do |format|
    format.html { redirect_to(feeds_url) }
    format.xml { head :ok }
  end
end

- (Object) display

GET /contents/1/display Trigger the render function a piece of content and passes all the params along for processing. Should send an inline result of the processing.



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'app/controllers/contents_controller.rb', line 222

def display
  # To support graphic preview where there isnt any content yet, create an unsaved
  # piece of content owned by the current user and associate the preview media with it.
  if params[:id] == "preview" && params[:type].present?
    get_content_const
    @content = @content_const.new(:user => current_user)
    media = Media.valid_preview(params[:media_id])
    if media.nil?
      raise ActiveRecord::RecordNotFound
    end
    @content.media << media
  else
    @content = Content.find(params[:id])
  end

  auth!(:action => :read)
  # if handling graphic preview (the content id is 0), force a render
  if params[:id] == "preview" || stale?(:etag => params, :last_modified => @content.updated_at.utc, :public => true)
    @file = nil
    data = nil
    benchmark("Content#render") do
      @file = @content.render(params)
      data = @file.file_contents
    end
    send_data data, :filename => @file.file_name, :type => @file.file_type, :disposition => 'inline'
  end
end

- (Object) edit

GET /contents/1/edit



86
87
88
89
90
# File 'app/controllers/contents_controller.rb', line 86

def edit
  @content = Content.find(params[:id])
  auth!
  @feeds = submittable_feeds
end

- (Object) get_content_const

Grab the constant object for the type of content we're working with. Probably needs additional error checking.



11
12
13
14
15
16
17
# File 'app/controllers/contents_controller.rb', line 11

def get_content_const
  begin
    @content_const = params[:type].camelize.constantize
  rescue
    @content_const = nil
  end
end

- (Object) index



19
20
21
22
23
24
25
26
27
# File 'app/controllers/contents_controller.rb', line 19

def index
  @content = Content.filter_all_content(params)
  @content = Kaminari.paginate_array(@content).page(params[:page])

  respond_to do |format|
    format.html
    format.js {render :json => @content}
  end
end

- (Object) new

GET /contents/new GET /contents/new.xml Instantiate a new object of params. If the object isn't valid (FooBar) or isn't a child of Content (Feed) a 400 error is thrown.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/contents_controller.rb', line 51

def new
  # We might already have a content type,
  if @content_const.nil? || !@content_const.ancestors.include?(Content)
    Rails.logger.debug "Content type #{@content_const} found not OK, trying default."
    default_upload_type = ConcertoConfig[:default_upload_type]
    if !default_upload_type
      raise t(:missing_default_type)
    else
      @content_const = default_upload_type.camelize.constantize
    end
  end

  # We don't recognize the requested content type, or
  # its not a child of Content so we'll return a 400.
  if @content_const.nil? || !@content_const.ancestors.include?(Content)
    render :text => t(:unrecognized_type), :status => 400
  else
    @content = @content_const.new()
    @content.duration = ConcertoConfig[:default_content_duration].to_i
    auth!
    @feeds = submittable_feeds
    respond_with(@content)
  end
end

- (Object) preview

returns the content types preview of the specified data or looked up by id



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'app/controllers/contents_controller.rb', line 279

def preview
  data = ""
  if !params[:data].nil?
    data = params[:data]
  elsif !params[:id].nil?
    content = Content.find(params[:id])
    data = content[:data] unless content.nil?
  end

  html = "Unrecognized content type"
  if !@content_const.nil?
    html = @content_const.preview(data)
  end
  respond_to do |format|
    format.html { render :text => html, :layout => false }
  end
end

- (Object) show

GET /contents/1 GET /contents/1.xml



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/contents_controller.rb', line 31

def show
  @content = Content.find(params[:id])
  @user = User.find(@content.user_id)
  auth!

  respond_with(@content)

rescue ActiveRecord::RecordNotFound
  # while it could be returned as a 404, we should keep the user in the application
  # render :text => "Requested content not found", :status => 404
  respond_to do |format|
    format.html { redirect_to(browse_path, :notice => t(:content_not_found)) }
  end
end

- (Object) update

PUT /contents/1 PUT /contents/1.xml



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'app/controllers/contents_controller.rb', line 164

def update
  @content = Content.find(params[:id])
  @user = User.find(@content.user_id)
  auth!

  @feed_ids = feed_ids

  if @content.update_attributes(content_update_params)
    process_notification(@content, {}, process_notification_options({
      :params => {
        :content_name => @content.name,
        :content_type => @content.class.model_name.human
      },
      :key => "content.#{action_name}"}))
    submissions = @content.submissions
    submissions.each do |submission|
      if @feed_ids.include? submission.feed_id
        submission.update_attributes(:moderation_flag => nil)
      else
        submission.mark_for_destruction
      end
    end
     = submissions.map { |s| s.feed_id }
    @feed_ids.reject! { |id| .include? id }
    create_submissions
    if @content.save
      flash[:notice] = t(:content_updated)
    end
    respond_with(@content)
  else
    @feeds = submittable_feeds
    respond_with(@content)
  end
end