Permalink
Browse files

いろんな修正が1コミットで反省している

  • Loading branch information...
1 parent 88ab294 commit fc21fc6ee46b6ca06439a5276ca9abd04a7ac2dc @suginoy committed Dec 26, 2011
Showing with 773 additions and 80 deletions.
  1. +3 −0 app/assets/javascripts/courses.js.coffee
  2. +3 −0 app/assets/javascripts/dashboard.js.coffee
  3. +3 −0 app/assets/stylesheets/courses.css.scss
  4. +3 −0 app/assets/stylesheets/dashboard.css.scss
  5. +85 −0 app/controllers/courses_controller.rb
  6. +7 −0 app/controllers/dashboard_controller.rb
  7. +24 −48 app/controllers/questions_controller.rb
  8. +10 −0 app/helpers/courses_helper.rb
  9. +2 −0 app/helpers/dashboard_helper.rb
  10. +10 −0 app/models/course.rb
  11. +30 −5 app/models/question.rb
  12. +0 −1 app/models/summary.rb
  13. +2 −1 app/models/user.rb
  14. +29 −0 app/views/courses/_form.html.erb
  15. +6 −0 app/views/courses/edit.html.erb
  16. +23 −0 app/views/courses/index.html.erb
  17. +5 −0 app/views/courses/new.html.erb
  18. +44 −0 app/views/courses/show.html.erb
  19. +22 −0 app/views/dashboard/show.html.erb
  20. +1 −0 app/views/layouts/application.html.erb
  21. +2 −4 app/views/questions/_form.html.erb
  22. +2 −2 app/views/questions/edit.html.erb
  23. +1 −1 app/views/questions/new.html.erb
  24. +10 −6 app/views/questions/show.html.erb
  25. +1 −0 app/views/shared/_header.html.erb
  26. +9 −6 config/routes.rb
  27. +12 −0 db/migrate/20111224011150_create_courses.rb
  28. +5 −0 db/migrate/20111224012654_add_course_id_to_questions.rb
  29. +5 −0 db/migrate/20111226132234_add_counter_cache_to_courses.rb
  30. +5 −0 db/migrate/20111226135810_add_sharing_to_courses.rb
  31. +14 −1 db/schema.rb
  32. +47 −5 db/seeds.rb
  33. +157 −0 spec/controllers/cources_controller_spec.rb
  34. +12 −0 spec/controllers/dashboard_controller_spec.rb
  35. +15 −0 spec/helpers/cources_helper_spec.rb
  36. +15 −0 spec/helpers/dashboard_helper_spec.rb
  37. +5 −0 spec/models/cource_spec.rb
  38. +11 −0 spec/requests/cources_spec.rb
  39. +35 −0 spec/routing/cources_routing_spec.rb
  40. +22 −0 spec/views/cources/edit.html.erb_spec.rb
  41. +28 −0 spec/views/cources/index.html.erb_spec.rb
  42. +22 −0 spec/views/cources/new.html.erb_spec.rb
  43. +21 −0 spec/views/cources/show.html.erb_spec.rb
  44. +5 −0 spec/views/dashboard/index.html.erb_spec.rb
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
@@ -0,0 +1,3 @@
+// Place all the styles related to the courses controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,3 @@
+// Place all the styles related to the dashboard controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,85 @@
+class CoursesController < ApplicationController
+ # GET /courses
+ # GET /courses.json
+ def index
+ @courses = Course.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render json: @courses }
+ end
+ end
+
+ # GET /courses/1
+ # GET /courses/1.json
+ def show
+ @course = Course.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render json: @course }
+ end
+ end
+
+ # GET /courses/new
+ # GET /courses/new.json
+ def new
+ @course = Course.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render json: @course }
+ end
+ end
+
+ # GET /courses/1/edit
+ def edit
+ @course = Course.find(params[:id])
+ end
+
+ # POST /courses
+ # POST /courses.json
+ def create
+ @course = Course.new(params[:course])
+ @course.owner_id = current_user.id
+
+ respond_to do |format|
+ if @course.save
+ format.html { redirect_to @course, notice: 'Course was successfully created.' }
+ format.json { render json: @course, status: :created, location: @course }
+ else
+ format.html { render action: "new" }
+ format.json { render json: @course.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /courses/1
+ # PUT /courses/1.json
+ def update
+ @course = Course.find(params[:id])
+ @course.owner_id = current_user.id
+
+ respond_to do |format|
+ if @course.update_attributes(params[:course])
+ format.html { redirect_to @course, notice: 'Course was successfully updated.' }
+ format.json { head :ok }
+ else
+ format.html { render action: "edit" }
+ format.json { render json: @course.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /courses/1
+ # DELETE /courses/1.json
+ def destroy
+ @course = Course.find(params[:id])
+ @course.destroy
+
+ respond_to do |format|
+ format.html { redirect_to courses_url }
+ format.json { head :ok }
+ end
+ end
+end
@@ -0,0 +1,7 @@
+class DashboardController < ApplicationController
+ def index
+ @user = current_user
+ @shared_courses = Course.shared_courses_for(@user)
+ end
+
+end
@@ -1,21 +1,12 @@
class QuestionsController < ApplicationController
- # GET /questions
- # GET /questions.json
- def index
- # @questions = Question.all
-
- respond_to do |format|
- format.html # index.html.erb
- format.json { render json: @questions }
- end
- end
-
- # GET /questions/1
- # GET /questions/1.json
+ # GET /courses/:course_id/questions/1
+ # GET /courses/:course_id/questions/1.json
def show
@user = current_user
@question = Question.find(params[:id])
- @next_question = Question.other_question(params[:id])
+ #@next_question = Question.other_question(params[:id])
+ @next_question = @question.next
+ @user.prepare_summary(@question[:id])
@summary = Summary.find_by_user_id_and_question_id(@user[:id], @question[:id])
respond_to do |format|
@@ -24,46 +15,43 @@ def show
end
end
- # GET /questions/new
- # GET /questions/new.json
+ # GET /courses/:course_id/questions/new
+ # GET /courses/:course_id/questions/new.json
def new
- @question = Question.new
-
- respond_to do |format|
- format.html # new.html.erb
- format.json { render json: @question }
- end
+ @question = Question.new(:course_id => params[:course_id])
end
- # GET /questions/1/edit
+ # GET /courses/:course_id/questions/1/edit
def edit
@question = Question.find(params[:id])
end
- # POST /questions
- # POST /questions.json
+ # POST /courses/:course_id/questions
+ # POST /courses/:course_id/questions.json
def create
- @question = Question.new(params[:question])
+ @course = Course.find(params[:course_id])
+ @question = @course.questions.build(params[:question])
+ @question.save
respond_to do |format|
if @question.save
- format.html { redirect_to @question, notice: 'Question was successfully created.' }
- format.json { render json: @question, status: :created, location: @question }
+ format.html { redirect_to @course, notice: 'Question was successfully created.' }
+ format.json { render json: @course, status: :created, location: @question }
else
format.html { render action: "new" }
format.json { render json: @question.errors, status: :unprocessable_entity }
end
end
end
- # PUT /questions/1
- # PUT /questions/1.json
+ # PUT /courses/:course_id/questions/1
+ # PUT /courses/:course_id/questions/1.json
def update
@question = Question.find(params[:id])
respond_to do |format|
if @question.update_attributes(params[:question])
- format.html { redirect_to @question, notice: 'Question was successfully updated.' }
+ format.html { redirect_to @question.course, notice: 'Question was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
@@ -72,8 +60,8 @@ def update
end
end
- # DELETE /questions/1
- # DELETE /questions/1.json
+ # DELETE /courses/:course_id/questions/1
+ # DELETE /courses/:course_id/questions/1.json
def destroy
@question = Question.find(params[:id])
@question.destroy
@@ -84,9 +72,10 @@ def destroy
end
end
- # POST /questions/1/answer
- # POST /questions/1/answer.json
+ # POST /courses/:course_id/questions/1/answer
+ # POST /courses/:course_id/questions/1/answer.json
def answer
+ # TODO: サマリ更新ロジックをモデルに移動
@question = Question.find(params[:id])
@user = current_user
@summary = Summary.find_by_user_id_and_question_id(@user[:id], @question[:id])
@@ -104,17 +93,4 @@ def answer
@summary.save!
@score.save!
end
-
- # GET /questions/1
- # GET /questions/1.json
- def first
- @user = current_user
-
- # サマリ全コピー TODO 1.バルクINSERTに変更(できるの?) / 2.初回ログイン時に変更する
- Question.all.each do |q|
- @user.prepare_summary(q[:id])
- end
-
- redirect_to :action => "show", :id => Summary.least_answered(@user[:id])[:question_id]
- end
end
@@ -0,0 +1,10 @@
+# encoding: utf-8
+module CoursesHelper
+ def sharing_display(sharing)
+ if sharing
+ "共有"
+ else
+ "専有"
+ end
+ end
+end
@@ -0,0 +1,2 @@
+module DashboardHelper
+end
View
@@ -0,0 +1,10 @@
+class Course < ActiveRecord::Base
+ belongs_to :owner, :class_name => 'User', :dependent => :delete
+ has_many :questions
+
+ validates :name, :presence => true
+ validates :description, :presence => true
+
+ scope :shared, where(:sharing => true)
+ scope :shared_courses_for, lambda { |user| shared.where 'owner_id != ?', user[:id] }
+end
View
@@ -1,9 +1,34 @@
class Question < ActiveRecord::Base
+ before_save :set_next_number
+ after_destroy :compact
+ belongs_to :course, :counter_cache => true
has_many :scores
has_many :users, :through => :scores
- default_scope order("id")
- scope :other_questions, lambda { |previous_id| where("id != ?", previous_id) }
- def self.other_question(id)
- self.other_questions(id).sample
- end
+
+ #validates :number, :title, :description, :choice1, :choice2, :choice3, :choice4, :explanation,
+ validates :title, :description, :choice1, :choice2, :choice3, :choice4, :explanation,
+ :presence => true
+ validates :answer, :inclusion => { :in => [1, 2, 3, 4] }
+
+ default_scope order("number")
+
+ def next
+ unless self.number == self.course.questions.length
+ Question.find_by_course_id_and_number(self.course_id, self.number + 1)
+ end
+ end
+
+ def compact
+ Question.where(:course_id => self.course_id).each do |question|
+ if question.number > self.number
+ question.update_atribute(:number, self.number - 1)
+ end
+ end
+ end
+
+ private
+ def set_next_number # TODO メソッド名変更
+ self.number ||= self.course.questions_count + 1
+ #self.number ||= Course.find(self.course_id).questions_count + 1
+ end
end
View
@@ -1,6 +1,5 @@
class Summary < ActiveRecord::Base
has_many :scores
- #has_many :users, :through => :scores
belongs_to :users
has_many :questions, :through => :scores
View
@@ -1,7 +1,7 @@
class User < ActiveRecord::Base
+ has_many :courses, :foreign_key => 'owner_id'
has_many :scores
has_many :questions, :through => :scores
- #has_many :summaries, :through => :scores # user_id をSummaryに追加して直接参照する
has_many :summaries
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
@@ -18,4 +18,5 @@ def prepare_summary(question_id)
Summary.find_by_user_id_and_question_id(self.id, question_id)
end
end
+
end
@@ -0,0 +1,29 @@
+<%= form_for(@course) do |f| %>
+ <% if @course.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@course.errors.count, "error") %> prohibited this course from being saved:</h2>
+
+ <ul>
+ <% @course.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="field">
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </div>
+ <div class="field">
+ <%= f.label :description %><br />
+ <%= f.text_area :description %>
+ </div>
+ <div class="field">
+ <%= f.label :sharing %><br />
+ <%= f.check_box :sharing %>
+ </div>
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
@@ -0,0 +1,6 @@
+<h1>Editing course</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @course %> |
+<%= link_to 'Back', courses_path %>
@@ -0,0 +1,23 @@
+<h1>Listing courses</h1>
+
+<table>
+ <tr>
+ <th>コース名</th>
+ <th>作成者</th>
+ <th>問題数</th>
+ <th>共有/専有</th>
+ </tr>
+
+<% @courses.each do |course| %>
+ <tr>
+ <td><%= link_to course.name, course %></td>
+ <td><%= course.owner.email %></td>
+ <td><%= course.questions_count %></td>
+ <td><%= course.sharing %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New Cource', new_course_path %>
@@ -0,0 +1,5 @@
+<h1>New course</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', courses_path %>
Oops, something went wrong.

0 comments on commit fc21fc6

Please sign in to comment.