From d3dd0ff848b31fbd30abec73b2191c0bc27d474f Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Mon, 13 Jul 2015 02:14:35 -0400 Subject: [PATCH] Add the subcreddit MVC Notes on closing subcreddit: This doesn't seem great. Would be slightly better if closed could be referenced as true or false instead of '0' or '1' in the model. Testing this is a bit messy, but Tiemcop makes it a bit better. Notes on subcreddit validations: This is still not ideal. For starters, from http://guides.rubyonrails.org/active_record_validations.html: "Note that some databases are configured to perform case-insensitive searches anyway." It also seems difficult to validate fields that are set in before_save since validations occur before callbacks. Revisit this (perhaps friendly_id wll save us). --- Gemfile | 1 + Gemfile.lock | 2 + .../{tests.coffee => subcreddits.coffee} | 0 .../{tests.scss => subcreddits.scss} | 2 +- app/controllers/subcreddits_controller.rb | 50 ++++++ app/controllers/tests_controller.rb | 7 - app/models/subcreddit.rb | 53 ++++++ app/views/application/_navbar.html.slim | 15 +- app/views/subcreddits/_form.html.slim | 8 + app/views/subcreddits/edit.html.slim | 1 + app/views/subcreddits/index.html.slim | 7 + app/views/subcreddits/new.html.slim | 1 + app/views/subcreddits/show.html.slim | 3 + app/views/subcreddits/update.html.slim | 0 app/views/tests/index.html.slim | 1 - app/views/tests/show.html.slim | 1 - config/routes.rb | 7 +- .../20150713045745_create_subcreddits.rb | 12 ++ db/schema.rb | 13 +- .../subcreddits_controller_spec.rb | 156 ++++++++++++++++++ spec/factories/subcreddit_factory.rb | 6 + spec/models/subcreddit_spec.rb | 126 ++++++++++++++ 22 files changed, 450 insertions(+), 22 deletions(-) rename app/assets/javascripts/{tests.coffee => subcreddits.coffee} (100%) rename app/assets/stylesheets/{tests.scss => subcreddits.scss} (62%) create mode 100644 app/controllers/subcreddits_controller.rb delete mode 100644 app/controllers/tests_controller.rb create mode 100644 app/models/subcreddit.rb create mode 100644 app/views/subcreddits/_form.html.slim create mode 100644 app/views/subcreddits/edit.html.slim create mode 100644 app/views/subcreddits/index.html.slim create mode 100644 app/views/subcreddits/new.html.slim create mode 100644 app/views/subcreddits/show.html.slim create mode 100644 app/views/subcreddits/update.html.slim delete mode 100644 app/views/tests/index.html.slim delete mode 100644 app/views/tests/show.html.slim create mode 100644 db/migrate/20150713045745_create_subcreddits.rb create mode 100644 spec/controllers/subcreddits_controller_spec.rb create mode 100644 spec/factories/subcreddit_factory.rb create mode 100644 spec/models/subcreddit_spec.rb diff --git a/Gemfile b/Gemfile index 390be6e..05d558a 100644 --- a/Gemfile +++ b/Gemfile @@ -50,4 +50,5 @@ group :test do gem 'capybara-webkit' gem 'shoulda' gem 'simplecov', require: false + gem 'timecop' end diff --git a/Gemfile.lock b/Gemfile.lock index ac3e8b6..016bbb5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -338,6 +338,7 @@ GEM thor (0.19.1) thread_safe (0.3.5) tilt (1.4.1) + timecop (0.7.4) tzinfo (1.2.2) thread_safe (~> 0.1) uglifier (2.7.1) @@ -392,6 +393,7 @@ DEPENDENCIES spring spring-commands-rspec sqlite3 + timecop uglifier (>= 1.3.0) BUNDLED WITH diff --git a/app/assets/javascripts/tests.coffee b/app/assets/javascripts/subcreddits.coffee similarity index 100% rename from app/assets/javascripts/tests.coffee rename to app/assets/javascripts/subcreddits.coffee diff --git a/app/assets/stylesheets/tests.scss b/app/assets/stylesheets/subcreddits.scss similarity index 62% rename from app/assets/stylesheets/tests.scss rename to app/assets/stylesheets/subcreddits.scss index 7e46f05..ed4afca 100644 --- a/app/assets/stylesheets/tests.scss +++ b/app/assets/stylesheets/subcreddits.scss @@ -1,3 +1,3 @@ -// Place all the styles related to the Tests controller here. +// Place all the styles related to the Subcreddits controller here. // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/subcreddits_controller.rb b/app/controllers/subcreddits_controller.rb new file mode 100644 index 0000000..256a6f1 --- /dev/null +++ b/app/controllers/subcreddits_controller.rb @@ -0,0 +1,50 @@ +class SubcredditsController < ApplicationController + before_filter :set_subcreddit, only: [:show, :edit, :update] + + def index + @subcreddits = Subcreddit.all + end + + def show + end + + def new + @subcreddit = Subcreddit.new + end + + def create + @subcreddit = Subcreddit.new(create_subcreddit_params) + @subcreddit.owner = current_user + + if @subcreddit.save + redirect_to @subcreddit, notice: 'Subcreddit was created!' + else + render :new + end + end + + def edit + end + + def update + if @subcreddit.update(update_subcreddit_params) + redirect_to @subcreddit, notice: 'Subcreddit was updated!' + else + render :edit + end + end + + private + + def create_subcreddit_params + params.require(:subcreddit).permit(:name) + end + + def update_subcreddit_params + params.require(:subcreddit).permit(:closed) + end + + def set_subcreddit + @subcreddit = Subcreddit.find_by_slug(params[:id]) + end +end diff --git a/app/controllers/tests_controller.rb b/app/controllers/tests_controller.rb deleted file mode 100644 index 4faf46f..0000000 --- a/app/controllers/tests_controller.rb +++ /dev/null @@ -1,7 +0,0 @@ -class TestsController < ApplicationController - def index - end - - def show - end -end diff --git a/app/models/subcreddit.rb b/app/models/subcreddit.rb new file mode 100644 index 0000000..e481b30 --- /dev/null +++ b/app/models/subcreddit.rb @@ -0,0 +1,53 @@ +RESERVED_SLUGS = %w(new edit) + +class Subcreddit < ActiveRecord::Base + belongs_to :owner, class_name: 'User' + + attr_accessor :closed + + before_save :set_slug + before_save :set_closed_at + + validates :name, + presence: true, + format: /\A(?! )[a-z0-9 ]*(?