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 ]*(?