Created
May 6, 2014 13:31
-
-
Save sandipransing/99f4ee979ff3c821d886 to your computer and use it in GitHub Desktop.
Coding exercise 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/app/controllers/episodes_controller.rb b/app/controllers/episodes_controller.rb | |
| index e845969..0a9c282 100644 | |
| --- a/app/controllers/episodes_controller.rb | |
| +++ b/app/controllers/episodes_controller.rb | |
| @@ -8,12 +8,18 @@ class EpisodesController < ApplicationController | |
| else | |
| @episodes = Episode.search_published(params[:search], params[:tag_id]) | |
| end | |
| + if params[:nsfw].present? | |
| + session[:nsfw] = params[:nsfw] == 'show' ? true : nil | |
| + end | |
| + if session[:nsfw].blank? | |
| + @episodes = @episodes.not_nsfw | |
| + end | |
| respond_to do |format| | |
| format.html { @episodes = @episodes.paginate(:page => params[:page], :per_page => episodes_per_page) } | |
| format.rss | |
| end | |
| end | |
| - | |
| + | |
| def show | |
| if params[:id] != @episode.to_param | |
| headers["Status"] = "301 Moved Permanently" | |
| @@ -47,7 +53,7 @@ class EpisodesController < ApplicationController | |
| render :edit | |
| end | |
| end | |
| - | |
| + | |
| private | |
| def episodes_per_page | |
| diff --git a/app/models/episode.rb b/app/models/episode.rb | |
| index 5f1f996..ed7fe4b 100644 | |
| --- a/app/models/episode.rb | |
| +++ b/app/models/episode.rb | |
| @@ -5,6 +5,7 @@ class Episode < ActiveRecord::Base | |
| has_paper_trail | |
| + scope :not_nsfw, where(:nsfw => false) | |
| scope :published, lambda { where('published_at <= ?', Time.now.utc) } | |
| scope :unpublished, lambda { where('published_at > ?', Time.now.utc) } | |
| scope :tagged, lambda { |tag_id| tag_id ? joins(:taggings).where(:taggings => {:tag_id => tag_id}) : scoped } | |
| diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb | |
| index 5ae7b8d..b961cb3 100644 | |
| --- a/app/views/layouts/application.html.erb | |
| +++ b/app/views/layouts/application.html.erb | |
| @@ -37,6 +37,8 @@ | |
| <div id="nav_bar"> | |
| <ul class="nav horizontal"> | |
| <li><%= link_to_unless_current "Browse Episodes", root_url %></li> | |
| + <% nsfw = session[:nsfw].present? ? 'hide' : 'show' %> | |
| + <li><%= link_to_unless_current "NSFW", nsfw_episodes_url(nsfw) %></li> | |
| <li><%= link_to_unless_current "About RailsCasts", about_path %></li> | |
| <li><%= link_to_unless_current "Feedback", feedback_path %></li> | |
| <% if can? :index, :comments %> | |
| diff --git a/config/routes.rb b/config/routes.rb | |
| index 0d6e07b..8c2ba84 100644 | |
| --- a/config/routes.rb | |
| +++ b/config/routes.rb | |
| @@ -16,6 +16,7 @@ Railscasts::Application.routes.draw do | |
| member { put :ban } | |
| end | |
| resources :comments | |
| + get 'episodes/:nsfw/nsfw' => 'episodes#index', :as => :nsfw_episodes | |
| resources :episodes | |
| resources :feedback_messages | |
| diff --git a/db/schema.rb b/db/schema.rb | |
| index 6aef037..f3ed5f7 100644 | |
| --- a/db/schema.rb | |
| +++ b/db/schema.rb | |
| @@ -11,7 +11,7 @@ | |
| # | |
| # It's strongly recommended to check this file into your version control system. | |
| -ActiveRecord::Schema.define(:version => 20110725215614) do | |
| +ActiveRecord::Schema.define(:version => 20140506124344) do | |
| create_table "comments", :force => true do |t| | |
| t.integer "episode_id" | |
| @@ -47,6 +47,7 @@ ActiveRecord::Schema.define(:version => 20110725215614) do | |
| t.boolean "asciicasts", :default => false, :null => false | |
| t.boolean "legacy", :default => false, :null => false | |
| t.text "file_sizes" | |
| + t.boolean "nsfw", :default => false | |
| end | |
| create_table "feedback_messages", :force => true do |t| | |
| diff --git a/spec/models/episode_spec.rb b/spec/models/episode_spec.rb | |
| index 455a3f0..5f08384 100644 | |
| --- a/spec/models/episode_spec.rb | |
| +++ b/spec/models/episode_spec.rb | |
| @@ -14,6 +14,15 @@ describe Episode do | |
| Episode.unpublished.should include(b) | |
| Episode.unpublished.should_not include(a) | |
| end | |
| + | |
| + it "should not list nsfw flagged", focus: true do | |
| + a = Factory(:episode, :published_at => 2.weeks.ago, :nsfw => true) | |
| + b = Factory(:episode, :published_at => 2.weeks.from_now) | |
| + a = Factory(:episode, :published_at => 1.weeks.ago) | |
| + | |
| + Episode.not_nsfw.count.should == 2 | |
| + Episode.not_nsfw.all.should == [b,a] | |
| + end | |
| it "sorts recent episodes in descending order" do | |
| Episode.delete_all | |
| diff --git a/spec/requests/episodes_request_spec.rb b/spec/requests/episodes_request_spec.rb | |
| index 2b8b4d4..d85b8d3 100644 | |
| --- a/spec/requests/episodes_request_spec.rb | |
| +++ b/spec/requests/episodes_request_spec.rb | |
| @@ -9,6 +9,23 @@ describe "Episodes request" do | |
| page.should_not have_content("Back to the Future") | |
| end | |
| + it "list episodes with nsfw flag", :focus => true do | |
| + Factory(:episode, :name => "Blast1", :published_at => 4.days.ago) | |
| + Factory(:episode, :name => "Blast2", :published_at => 2.weeks.ago) | |
| + Factory(:episode, :name => "Blast3", :published_at => 5.months.ago, :nsfw => true) | |
| + Factory(:episode, :name => "Back to the Future", :published_at => 2.days.from_now, :nsfw => true) | |
| + | |
| + visit nsfw_episodes_path('show') | |
| + page.should have_selector('.episode') | |
| + all('.episode').size.should == 3 | |
| + | |
| + episodes_names = all('.episode').map { |episode| episode.find('h2').text } | |
| + (%w(Blast1 Blast2 Blast3) - episodes_names).size.should == 0 | |
| + | |
| + click_link('NSFW') | |
| + save_and_open_page | |
| + end | |
| + | |
| it "provides RSS feed of episodes" do | |
| Factory(:episode, :name => "Blast from the Past", :published_at => 2.days.ago) | |
| visit episodes_path(:format => :rss) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment