From 25f09a3515db24b2a9a16751d2dbc60bd57a7e0f Mon Sep 17 00:00:00 2001 From: Adrian Hinz Date: Sat, 11 May 2019 08:05:16 +0200 Subject: [PATCH] weeks in progress --- app/controllers/admin/courses_controller.rb | 2 +- app/controllers/admin/weeks_controller.rb | 87 +++++++++++++++++++ app/models/course.rb | 2 + app/models/week.rb | 6 ++ spec/routing/admin/courses_routing_spec.rb | 37 ++++---- .../views/admin/courses/edit.html.erb_spec.rb | 17 ++-- .../admin/courses/index.html.erb_spec.rb | 26 +++--- spec/views/admin/courses/new.html.erb_spec.rb | 17 ++-- .../views/admin/courses/show.html.erb_spec.rb | 10 +-- 9 files changed, 148 insertions(+), 56 deletions(-) create mode 100644 app/controllers/admin/weeks_controller.rb diff --git a/app/controllers/admin/courses_controller.rb b/app/controllers/admin/courses_controller.rb index 61beeeb..d7e20bf 100644 --- a/app/controllers/admin/courses_controller.rb +++ b/app/controllers/admin/courses_controller.rb @@ -76,7 +76,7 @@ module Admin end def collection - @courses = Course.all + @courses = Course.by_name end # Never trust parameters from the scary internet, only allow the white list through. diff --git a/app/controllers/admin/weeks_controller.rb b/app/controllers/admin/weeks_controller.rb new file mode 100644 index 0000000..938b0e9 --- /dev/null +++ b/app/controllers/admin/weeks_controller.rb @@ -0,0 +1,87 @@ +# frozen_string_literal: true + +module Admin + # Kursy + class WeeksController < ApplicationController + before_action :set_object, only: %i[show edit update destroy] + + # GET /admin/weeks + # GET /admin/weeks.json + def index + collection + end + + # GET /admin/weeks/1 + # GET /admin/weeks/1.json + def show; end + + # GET /admin/weeks/new + def new + @week = Week.new(course_id: params[:course_id]) + end + + # GET /admin/weeks/1/edit + def edit; end + + # POST /admin/weeks + # POST /admin/weeks.json + def create + @week = Week.new(admin_week_params) + + respond_to do |format| + if @week.save + format.js { collection } + format.html { redirect_to [:admin, @week], notice: 'Week was successfully created.' } + format.json { render :show, status: :created, location: @week } + else + format.js { render :new } + format.html { render :new } + format.json { render json: @week.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /admin/weeks/1 + # PATCH/PUT /admin/weeks/1.json + def update + respond_to do |format| + if @week.update(admin_week_params) + format.js { collection } + format.html { redirect_to [:admin, @week], notice: 'Week was successfully updated.' } + format.json { render :show, status: :ok, location: @week } + else + format.js { render :edit } + format.html { render :edit } + format.json { render json: @week.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /admin/weeks/1 + # DELETE /admin/weeks/1.json + def destroy + @week.destroy + respond_to do |format| + format.js { collection } + format.html { redirect_to admin_weeks_url, notice: 'Week was successfully destroyed.' } + format.json { head :no_content } + end + end + + private + + # Use callbacks to share common setup or constraints between actions. + def set_object + @week = Week.find(params[:id]) + end + + def collection + @weeks = Week.by_course(params[:course_id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def admin_week_params + params.require(:week).permit(:name, :description) + end + end +end diff --git a/app/models/course.rb b/app/models/course.rb index 5c9d422..0c1e790 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -3,4 +3,6 @@ class Course < ApplicationRecord has_many :weeks, dependent: :destroy validates :name, presence: true + + scope :name_asc, -> { order('name ASC') } end diff --git a/app/models/week.rb b/app/models/week.rb index 6f786dd..25ecc48 100644 --- a/app/models/week.rb +++ b/app/models/week.rb @@ -1,3 +1,9 @@ +# frozen_string_literal: true + class Week < ApplicationRecord belongs_to :course + + scope :by_course, ->(c_id) { where(course_id: c_id) } + scope :name_asc, -> { order('name ASC') } + scope :name_desc, -> { order('name DESC') } end diff --git a/spec/routing/admin/courses_routing_spec.rb b/spec/routing/admin/courses_routing_spec.rb index 8a60182..bd813af 100644 --- a/spec/routing/admin/courses_routing_spec.rb +++ b/spec/routing/admin/courses_routing_spec.rb @@ -1,38 +1,37 @@ -require "rails_helper" +require 'rails_helper' RSpec.describe Admin::CoursesController, type: :routing do - describe "routing" do - it "routes to #index" do - expect(:get => "/admin/courses").to route_to("admin/courses#index") + describe 'routing' do + it 'routes to #index' do + expect(get: '/admin/courses').to route_to('admin/courses#index') end - it "routes to #new" do - expect(:get => "/admin/courses/new").to route_to("admin/courses#new") + it 'routes to #new' do + expect(get: '/admin/courses/new').to route_to('admin/courses#new') end - it "routes to #show" do - expect(:get => "/admin/courses/1").to route_to("admin/courses#show", :id => "1") + it 'routes to #show' do + expect(get: '/admin/courses/1').to route_to('admin/courses#show', id: '1') end - it "routes to #edit" do - expect(:get => "/admin/courses/1/edit").to route_to("admin/courses#edit", :id => "1") + it 'routes to #edit' do + expect(get: '/admin/courses/1/edit').to route_to('admin/courses#edit', id: '1') end - - it "routes to #create" do - expect(:post => "/admin/courses").to route_to("admin/courses#create") + it 'routes to #create' do + expect(post: '/admin/courses').to route_to('admin/courses#create') end - it "routes to #update via PUT" do - expect(:put => "/admin/courses/1").to route_to("admin/courses#update", :id => "1") + it 'routes to #update via PUT' do + expect(put: '/admin/courses/1').to route_to('admin/courses#update', id: '1') end - it "routes to #update via PATCH" do - expect(:patch => "/admin/courses/1").to route_to("admin/courses#update", :id => "1") + it 'routes to #update via PATCH' do + expect(patch: '/admin/courses/1').to route_to('admin/courses#update', id: '1') end - it "routes to #destroy" do - expect(:delete => "/admin/courses/1").to route_to("admin/courses#destroy", :id => "1") + it 'routes to #destroy' do + expect(delete: '/admin/courses/1').to route_to('admin/courses#destroy', id: '1') end end end diff --git a/spec/views/admin/courses/edit.html.erb_spec.rb b/spec/views/admin/courses/edit.html.erb_spec.rb index 2547ee4..8f4be34 100644 --- a/spec/views/admin/courses/edit.html.erb_spec.rb +++ b/spec/views/admin/courses/edit.html.erb_spec.rb @@ -1,21 +1,20 @@ require 'rails_helper' -RSpec.describe "admin/courses/edit", type: :view do +RSpec.describe 'admin/courses/edit', type: :view do before(:each) do @admin_course = assign(:admin_course, Admin::Course.create!( - :name => "MyString", - :description => "MyText" - )) + name: 'MyString', + description: 'MyText' + )) end - it "renders the edit admin_course form" do + it 'renders the edit admin_course form' do render - assert_select "form[action=?][method=?]", admin_course_path(@admin_course), "post" do + assert_select 'form[action=?][method=?]', admin_course_path(@admin_course), 'post' do + assert_select 'input[name=?]', 'admin_course[name]' - assert_select "input[name=?]", "admin_course[name]" - - assert_select "textarea[name=?]", "admin_course[description]" + assert_select 'textarea[name=?]', 'admin_course[description]' end end end diff --git a/spec/views/admin/courses/index.html.erb_spec.rb b/spec/views/admin/courses/index.html.erb_spec.rb index 2b15056..33caf2b 100644 --- a/spec/views/admin/courses/index.html.erb_spec.rb +++ b/spec/views/admin/courses/index.html.erb_spec.rb @@ -1,22 +1,22 @@ require 'rails_helper' -RSpec.describe "admin/courses/index", type: :view do +RSpec.describe 'admin/courses/index', type: :view do before(:each) do assign(:admin_courses, [ - Admin::Course.create!( - :name => "Name", - :description => "MyText" - ), - Admin::Course.create!( - :name => "Name", - :description => "MyText" - ) - ]) + Admin::Course.create!( + name: 'Name', + description: 'MyText' + ), + Admin::Course.create!( + name: 'Name', + description: 'MyText' + ) + ]) end - it "renders a list of admin/courses" do + it 'renders a list of admin/courses' do render - assert_select "tr>td", :text => "Name".to_s, :count => 2 - assert_select "tr>td", :text => "MyText".to_s, :count => 2 + assert_select 'tr>td', text: 'Name'.to_s, count: 2 + assert_select 'tr>td', text: 'MyText'.to_s, count: 2 end end diff --git a/spec/views/admin/courses/new.html.erb_spec.rb b/spec/views/admin/courses/new.html.erb_spec.rb index 165a297..9148597 100644 --- a/spec/views/admin/courses/new.html.erb_spec.rb +++ b/spec/views/admin/courses/new.html.erb_spec.rb @@ -1,21 +1,20 @@ require 'rails_helper' -RSpec.describe "admin/courses/new", type: :view do +RSpec.describe 'admin/courses/new', type: :view do before(:each) do assign(:admin_course, Admin::Course.new( - :name => "MyString", - :description => "MyText" - )) + name: 'MyString', + description: 'MyText' + )) end - it "renders new admin_course form" do + it 'renders new admin_course form' do render - assert_select "form[action=?][method=?]", admin_courses_path, "post" do + assert_select 'form[action=?][method=?]', admin_courses_path, 'post' do + assert_select 'input[name=?]', 'admin_course[name]' - assert_select "input[name=?]", "admin_course[name]" - - assert_select "textarea[name=?]", "admin_course[description]" + assert_select 'textarea[name=?]', 'admin_course[description]' end end end diff --git a/spec/views/admin/courses/show.html.erb_spec.rb b/spec/views/admin/courses/show.html.erb_spec.rb index 316d1eb..5871844 100644 --- a/spec/views/admin/courses/show.html.erb_spec.rb +++ b/spec/views/admin/courses/show.html.erb_spec.rb @@ -1,14 +1,14 @@ require 'rails_helper' -RSpec.describe "admin/courses/show", type: :view do +RSpec.describe 'admin/courses/show', type: :view do before(:each) do @admin_course = assign(:admin_course, Admin::Course.create!( - :name => "Name", - :description => "MyText" - )) + name: 'Name', + description: 'MyText' + )) end - it "renders attributes in

" do + it 'renders attributes in

' do render expect(rendered).to match(/Name/) expect(rendered).to match(/MyText/)