C0 code coverage information

Generated on Fri Aug 25 10:11:37 PDT 2006 with rcov 0.7.0


Code reported as executed by Ruby looks like this...
and this: this line is also marked as covered.
Lines considered as run by rcov, but not reported by Ruby, look like this,
and this: these lines were inferred by rcov (using simple heuristics).
Finally, here's a line marked as not executed.
Name Total lines Lines of code Total coverage Code coverage
app/controllers/articles_controller.rb 609 474
48.3% 
41.6% 
  1 class ArticlesController < ApplicationController
  2     layout 'staff'
  3     model   :user
  4 
  5     @@ARTICLE_TYPES = %w{ RecordReview TrackReview News NewsBlurb Feature Download }
  6     COLS = %w{ title artist updated_at published_at approved rating }
  7 
  8     before_filter :check_editor_role, :only => [ :approve_article, :destroy ]
  9     before_filter :check_copy_editor_role, :only => [ :return_to_author ]
 10     before_filter :check_contrib_role, :only => [ :create, :create_from_assignment, :edit, :update, :pin_version, :add_image, :submit, :preview_article ]
 11     
 12     cache_sweeper :article_sweeper, :only => [:create, :update, :destroy, :pin_version, :create_from_assignment, :approve_article]
 13     
 14     def initialize
 15         super
 16 
 17         @@ARTICLE_TYPES.each do |name|
 18             eval <<-END_FUNC
 19             def new_#{ Inflector.underscore(name) }
 20                 new_article('#{ name }')
 21             end
 22             END_FUNC
 23         end
 24     end
 25 
 26     def index
 27         list
 28         render :action => 'list'
 29     end
 30 
 31     def list
 32         conditions = nil
 33         role_id = @session['user'].role_id
 34         if role_id >= UserSystem::USER_ROLE[:copy_editor]
 35             conditions = 'published_at >= now() OR (published_at IS NULL and approved != 1) or approved != 1'
 36         elsif UserSystem::USER_ROLE[:contributor]
 37             if params[:action] == 'list_current'
 38                 conditions = "(approved = 1 or author_id = #{ @session['user'].id }) and published_at >= now()"
 39             else
 40                 conditions = "(approved = 0 and author_id = #{ @session['user'].id }) or (author_id = #{ @session['user'].id } and published_at >= now())"
 41             end
 42         else
 43             if params[:action] == 'list_current'
 44                 conditions = "approved = 1 and published_at >= now()"
 45             else
 46                 conditions = "approved = 1"
 47             end
 48         end
 49         
 50         make_list(conditions, sort_columns(params[:sort]))
 51     end
 52     
 53     def list_current
 54         list
 55         render :action => 'list'
 56     end 
 57     
 58     def list_all
 59         conditions = nil
 60         role_id = @session['user'].role_id
 61         if role_id >= UserSystem::USER_ROLE[:copy_editor]
 62             conditions = nil
 63         elsif UserSystem::USER_ROLE[:contributor]
 64             conditions = "(approved = 1 or author_id = #{ @session['user'].id })"
 65         else
 66             conditions = "approved = 1"
 67         end 
 68 
 69         make_list(conditions, sort_columns(params[:sort]))
 70         render :action => 'list'
 71     end
 72 
 73     def show
 74         return false unless find_article
 75         author_or_copy_editor? false, false
 76         
 77         @version_number = 0 
 78         @head_version_number = @article.version
 79         if(params[:version])
 80             @version_number = params[:version].to_i
 81             @article.revert_to(@version_number) 
 82         else
 83             @version_number = @article.version
 84         end
 85         
 86         @read_only = true
 87     end
 88 
 89     def search
 90         if params[:search] and params[:search][:criteria]
 91             @criteria = params[:search][:criteria].strip
 92         elsif params[:criteria]
 93             @criteria = params[:criteria].strip
 94         else
 95             redirect_to :action => 'list'
 96         end
 97         
 98         @article_pages, @articles = paginate(
 99             :articles,
100             :select => "articles.id as id, type, title, artist, articles.updated_at, published_at, approved, abstract, rating",
101             :order => "artist, title ASC",
102             :joins => "LEFT OUTER JOIN users on users.id = articles.author_id",
103             :per_page => 20,
104             :order => sort_columns(params[:sort]),
105             :conditions => ["(title LIKE :criteria OR subtitle LIKE :criteria 
106             OR artist LIKE :criteria
107             OR bside_artist LIKE :criteria OR bside_title LIKE :criteria
108             OR record_label LIKE :criteria
109             OR custom_author LIKE :criteria
110             OR display_name LIKE :criteria)",
111             { :criteria => "%" << @criteria << "%" }])
112         #render :action => 'list'
113     end
114 
115     def approve_article
116         if approve(params[:id], nil)
117             flash[:notice] = 'Article was successfully approved. The article will now automatically publish on the specified publish date.'
118         else
119             flash[:error] = 'Was unable to approve article. Please check that the article is correct and still exists.'
120         end
121         redirect_to :action => 'list'
122     end
123 
124 
125     # Method to create a new Article. Create's it with the initiated type
126     # and creates the first version of the Article.
127     def create
128         params[:article].delete_if { |key, value| value.nil? or value == ""}
129         
130         @article = create_typed_article(params[:article]) # create the article object and set the parameters.
131         
132         # set the defaults for a Bloglike types.
133         if @article.class == NewsBlurb or @article.class == TrackReview
134             @article.submitted = 1 unless params[:article][:submitted]
135             #@article.approved = 1 unless params[:article][:approved]
136             @article.published_at = Time.now unless @article.published_at
137         end
138         
139         Article.transaction do
140             # handle the image if there is one.
141             handle_image
142 
143             @article.editor = @session['user']
144             @article.cache_dirty = 1
145             # set the submitted flag is they submitted it.
146             if params[:commit] == "Submit for Approval"
147                 @article.submitted = 1
148             end
149 
150             # if the author id isn't set from an assignment, set it to the current user
151             unless @article.author
152                 @article.author_id = @session['user'].id
153             end
154 
155             #fill_pages_and_links(@article, params[:article_page], params[:link], params[:media_links])
156 
157             # for the non-time sensitive info, set the time to 4:00AM
158             if (@article.class == RecordReview or @article.class == Feature) and @article.published_at
159                 @article.published_at = @article.published_at + (14400) # 14400 seconds = 4 hours
160             end
161 		
162             @article.attributes = params[:article]
163             fill_pages_and_links(@article, params[:article_page], params[:link], params[:media_link])
164 	
165             #@article.published_at = 
166             if @article.save
167                 if params[:commit] == "Submit for Approval" and @article.submitted == 1
168                     url = url_for(:action => 'show', :id => @article)
169                     UserNotify.deliver_submitted_article(@article, url)
170                     flash[:notice] = 'Article was successfully created and submitted for review.'
171                 else
172                     flash[:notice] = 'Article was successfully created.'
173                 end
174 
175                 redirect_to :action => 'list'
176             else
177                 render :action => 'new'
178             end
179         end
180     end
181 
182     def create_from_assignment
183         begin
184             @assignment = Assignment.find(params[:id])
185         rescue ActiveRecord::RecordNotFound
186             # we will redirect it to the oops page.
187             render :template => '/page/oops', :status     => "404 Not Found"
188             return false
189         end
190  
191         unless @session['user'][:id] == @assignment.user_id or @session['user'].role_id >= UserSystem::USER_ROLE[:copy_editor]
192             redirect_unauth "You are not the user assigned to specified assignment."
193             return false
194         end
195 
196         # if the assignment already has a created article, redirect to update
197         redirect_to :action => 'edit', :id => @assignment.article.id if @assignment.article
198         
199         @article_type = @assignment.article_type
200         @article = Object::const_get(@article_type).new
201 
202         @article.author_id = @assignment.user_id
203         @article.published_at = @assignment.planned_publish_date
204         @article.title = @assignment.title
205         @article.artist = @assignment.artist
206 
207         @page_count = 1
208         render :action => 'new'
209     end
210 
211     def edit
212         return false unless find_article
213         return false unless author_or_copy_editor? true, true
214         
215         if(params[:version])
216             @article.revert_to(params[:version])
217         end
218         @article_type = @article.class.to_s
219 
220         # set up the pages
221         @page_count = 1
222         if @article.article_pages.size > 0
223             @page_count = @article.article_pages.size
224         end
225     end
226 
227     # Creates a new version of the specified Article, from the 
228     # optionally specified version data. If no version is specified,
229     # it edits from the pinned_version.
230     def update
231         return false unless find_article
232         return false unless author_or_copy_editor? true, true
233         
234         if params[:article_version] and params[:article_version].to_i != @article.version
235             @article.revert_to(params[:article_version])
236         end
237         
238         # set the submitted flag is they submitted it.
239         if params[:commit] == "Submit for Approval"
240             @article.submitted = 1
241         end
242         
243         unless is_in_role? UserSystem::USER_ROLE[:editor]
244             editor_fields = [ 'is_special', 'published_at', 'approved', 'premiere_position' ]
245             params[:article].delete_if { |key, value| editor_fields.include?(key) }
246         end
247         
248         # prevent the empty url from destroying the save.
249         if params[:article][:old_path] and params[:article][:old_path] == ""
250             params[:article].delete(:old_path)
251             @article.old_path = nil
252         end
253 
254         # check if we need to set the date BNM added
255         if @article.class == RecordReview and @article.best_new_music != params[:article][:best_new_music].to_i
256             @article.best_new_music_added_at = Time.now
257         end
258         
259         @article.attributes = params[:article]
260         fill_pages_and_links(@article, params[:article_page], params[:link], params[:media_link])
261 
262         if @article.class == NewsBlurb or @article.class == TrackReview
263             @article.submitted = 1 unless params[:article][:submitted]
264             #@article.approved = 1 unless params[:article][:approved]
265             @article.published_at = Time.now unless @article.published_at
266         end
267 
268         # for the non-time sensitive info, set the time to 4:00AM
269         if @article.class == RecordReview or @article.class == Feature
270             @article.published_at = @article.published_at + (14400) # 14400 seconds = 4 hours
271         end
272 
273         Article.transaction do
274             # delete the images if the delete fields are checked.
275             @article.image.destroy if @article.image and params[:article][:image_id].nil?
276             @article.alt_image.destroy if @article.alt_image and params[:article][:alt_image_id].nil?
277             
278             # now handle the uploaded images.
279             handle_image 
280 
281             @article.cache_dirty = 1
282             
283             ##logger.info("Right before save @article: #{ @article.inspect }")
284             if @article.save
285                 #logger.info("Right after save @article: #{ @article.inspect }")
286                 if params[:commit] == "Submit for Approval" and @article.submitted == 1
287                     url = url_for(:action => 'show', :id => @article)
288                     UserNotify.deliver_submitted_article(@article, url)
289                     flash[:notice] = 'A new Article Version was successfully created and submitted for review.'
290                 else
291                     flash[:notice] = 'A new Article Version was successfully created.'
292                 end
293             
294                 redirect_to :action => 'show', :id => @article.id
295             else
296                 render :action => 'edit'
297             end
298         end
299     end
300 
301     def pin_version
302         return false unless find_article
303         return false unless author_or_copy_editor? true, true
304 
305         do_version_pinning(@article, params[:version])
306         redirect_to :action => 'show', :id => @article.id, :version => @article.version
307     end
308 
309     def destroy
310         begin
311             Article.find(params[:id]).destroy
312         rescue ActiveRecord::RecordNotFound
313             # we will redirect it to the oops page.
314             render :template => '/page/oops', :status     => "404 Not Found"
315             return false
316         end
317         redirect_to :action => 'list'
318     end
319 
320     def add_image
321         @image = Image.new(params[:image])
322         if @image.save
323             @message = "Image successfully saved."
324         else
325             @message = "Image failed to upload."
326         end
327     end
328     
329     def submit
330         return false unless find_article
331         return false unless author_or_copy_editor? true, true
332 
333         @article.submitted = 1
334         if @article.save
335             url = url_for(:action => 'show', :id => @article.id)
336             UserNotify.deliver_submitted_article(@article, url)
337 
338             flash[:notice] = 'Article was successfully submitted for review.'
339             redirect_to :action => 'list'
340         else
341             flash[:notice] = 'An error occured while trying to submit this article.'
342             render :action => 'edit'
343         end
344     end
345     
346     def return_to_author
347         return false unless find_article
348         
349         @article.submitted = 0
350         if @article.save
351             url = url_for(:action => 'show', :id => @article.id)
352             
353             # check and see if a notification needs to be sent to user
354             if @article.author and ((@article.author.notification_flags & UserNotify::NOTIFICATION_TYPES[:return_to_author]) != 0)
355                 UserNotify.deliver_return_to_author(@article, url)
356             end
357 
358             flash[:notice] = 'Article was successfully returned to the Author for a rewrite.'
359             redirect_to :action => 'list'
360         else
361             flash[:notice] = 'An error occured while trying to return this article to the Author.'
362             render :action => 'edit'
363         end
364     end
365 
366     def image_popup
367         @article_type = 'content'
368         if params[:id] and params[:id] != ""
369             begin
370                 @image = Image.find(params[:id])
371             rescue ActiveRecord::RecordNotFound
372                 @image = Image.new
373             end
374         else
375             @image = Image.new
376         end
377         
378         unless request.get?
379             image = Image.new_with_data(params[:image])
380             image.save
381             @image_url = image.url('image')
382             @image_title = image.title
383             @submitted = true
384         end
385         render :layout => "mce_popup", :partial => "image"
386     end
387     
388     private
389     
390     def find_article
391         begin
392             @article = Article.find(params[:id])
393             @article.editor = @session['user']
394         rescue ActiveRecord::RecordNotFound
395             # we will redirect it to the oops page.
396             render :template => '/page/oops', :status     => "404 Not Found"
397             return false
398         end
399     end
400     
401     def make_list(conditions, sort)
402         @page_data = []
403         
404          @@ARTICLE_TYPES.each do |name|
405             underscore_name = Inflector.underscore(name)
406             under_plural_name = Inflector.pluralize(underscore_name)
407             if conditions and sort
408               eval "@#{underscore_name}_pages, @#{under_plural_name} = paginate :#{underscore_name}, :conditions => '#{ conditions }', :order => '#{sort}', :per_page => 20"
409             elsif sort
410               eval "@#{underscore_name}_pages, @#{under_plural_name} = paginate :#{underscore_name}, :order => '#{sort}', :per_page => 20"
411             elsif conditions
412               eval "@#{underscore_name}_pages, @#{under_plural_name} = paginate :#{underscore_name}, :conditions => '#{ conditions }', :order => 'published_at ASC', :per_page => 20"
413             else
414                 eval "@#{underscore_name}_pages, @#{under_plural_name} = paginate :#{underscore_name}, :order => 'published_at ASC', :per_page => 20"
415             end
416         end
417         @article_types = @@ARTICLE_TYPES
418     end
419 
420     def create_typed_article(params)
421         article = nil
422         case params[:type]
423         when 'RecordReview'
424             article = RecordReview.new(params)
425         when 'TrackReview'
426             article = TrackReview.new(params)
427         when 'Download'
428             article = Download.new(params)
429         when 'News'
430             article = News.new(params)
431         when 'NewsBlurb'
432             article = NewsBlurb.new(params)
433         when 'Feature'
434             article = Feature.new(params)
435         end
436             article
437     end
438 
439     def new_article(article_type)
440         @article_type = article_type
441         @article = Object::const_get(article_type).new
442         @page_count = 1
443 
444         render :action => 'new'
445     end
446 
447     def handle_image()
448         #logger.info('Inside the handle_image method.')
449         # features can have 2 images, loop through the 2 possible form groups.
450         [:image, :home_image].each do | image_form |
451             
452             if params[image_form] and params[image_form][:image] and params[image_form][:image].length > 0
453                 
454                 image = Image.new_with_data(params[image_form])
455                 
456                 #check if there is an image in the form
457                 if image and image.image_data_id > 0
458                     #handle record reviews and features differently.
459                     case params[image_form][:image_type]
460                     when 'record_review'
461                         #logger.info("#{image_form} is a record_review")
462                         # we will resize the image for the record review so 
463                         # that it will fit constraints.
464                         image.set_constraints(150, 150)
465                         image.create_thumb(:big_thumb, 110, 110)
466                         image.create_thumb(:small_thumb, 91, 91)
467                         image.create_thumb(:tiny_thumb, 50, 50)
468                     when 'feature'
469                         #logger.info("#{image_form} is a feature")
470                         if image_form == :image
471                             image.set_constraints(525, nil)
472                             image.create_thumb(:big_thumb, 250, nil)
473                             image.create_thumb(:tiny_thumb, 130, nil)
474                         else
475                             image.set_constraints(250, nil)
476                         end
477                     when 'news'
478                         # make a big thum just in case it is too big for the home page gutter
479                         #logger.info("#{image_form} is a news")
480                         image.set_constraints(525, nil)
481                         image.create_thumb(:big_thumb, 252, nil)
482                     when 'track_review'
483                         #logger.info("#{image_form} is a track_review")
484                         if image_form == :image
485                             #make the record size pic for landing
486                             image.set_constraints(525, nil)
487                             image.create_thumb(:big_thumb, 252, nil)
488                             image.create_thumb(:small_thumb, 91, nil)
489                             image.create_thumb(:tiny_thumb, 50, nil)
490                         else
491                             image.set_constraints(252, nil)
492                         end
493                     end
494                     
495                     # store the image object in the article version
496                     image.save
497                     #logger.info("#{image_form} should be now saved. this is what it looks like: #{image}")
498                     if image_form == :image
499                         @image = image
500                         @article.image = @image
501                         #logger.info("@article.image = #{@article.image}")
502                     else
503                         @alt_image = image
504                         @article.alt_image = @alt_image
505                         #logger.info("@article.alt_image = #{@article.image}")
506                     end
507                 end
508             end
509         end
510     end
511 
512     def update_properties!(object, props)
513         props.each do |key, value|
514             object[key] = value
515         end
516     end
517 
518     def approve(id, version)
519         return false unless find_article
520         
521         if version
522             return false unless do_version_pinning(@article, version)
523         end
524         @article.editor = @session['user']
525         @article.update_attribute(:approved, 1)
526     end
527     
528     def do_version_pinning(article, version)
529         begin 
530             if @article
531                 @article.editor = @session['user']
532                 if article.revert_to!(params[:version])
533                     flash[:notice] = "Reverted Article to Version #{params[:version]} successfully."
534                 else
535                     flash[:error] = 'Error occured while trying to revert version.'
536                 end
537             end
538         rescue ActiveRecord::RecordNotFound
539             flash[:error] = "Could not find a version #{ version } to revert to."
540             return nil
541         end
542     end
543 
544     def fill_pages_and_links(article, pages_params, link_params, media_link_params)
545         article.article_pages = []
546         article.links = []
547         article.media_links = []
548 
549         if pages_params
550             pages_params.sort { |a, b| a[0].to_i <=> b[0].to_i}.each_with_index do |page_data, index|
551                 page_params = { :page_number => index + 1, :body => page_data[1][:body], :custom_title => page_data[1][:custom_title] }
552                 article_page = ArticlePage.new(page_params)
553                 article.article_pages << article_page
554             end
555         end
556         
557         if link_params
558             link_params.each do |link_number, link_data|
559                 if link_data['url'] and link_data['url'] != ""
560                     link = Link.new(link_data)
561                     article.links << link
562                 end
563             end
564         end
565 
566         if media_link_params
567             media_link_params.each do |link_number, link_data|
568                 if link_data['url'] and link_data['url'] != ""
569                     media_link = MediaLink.new(link_data)
570                     article.media_links << media_link
571                 end
572             end
573         end
574     end
575     
576     def author_or_copy_editor? (with_flash, with_redirect)
577         unless (@session['user'][:id] == @article.author_id) or (is_in_role? UserSystem::USER_ROLE[:copy_editor])
578             flash[:error] = "You are not the author of this article, and are not allowed to edit it." if with_flash
579             redirect_to :action => 'show', :id => @article.id if with_redirect
580             return false
581         end
582         unless (@article.submitted == 0) or (is_in_role? UserSystem::USER_ROLE[:copy_editor])
583             flash[:error] = "You have submitted this article, you can no longer make changes to it." if with_flash
584             redirect_to :action => 'show', :id => @article.id if with_redirect
585             return false
586         end
587         @editable = true
588         true
589     end
590 
591     # Generate SQL for clicking html table header to sort
592     def sort_columns(column)
593         sort = nil
594         
595         COLS.each do |col|
596             if column and column[col]
597                 sort = col
598                 if column["_reverse"]
599                   sort << " DESC"
600                 else 
601                   sort << " ASC"
602                 end
603                 break
604             end
605         end
606         return sort
607     end
608    
609 end

Generated using the rcov code coverage analysis tool for Ruby version 0.7.0.

Valid XHTML 1.0! Valid CSS!