diff --git a/app/models/user.rb b/app/models/user.rb index 019c14d..d9eb568 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -9,8 +9,6 @@ class User < ActiveRecord::Base has_many :checklists, through: :assign_histories serialize :extra_info - - scope :generation_sorted_desc, -> { order(generation_id: :desc) } scope :responsed_to_notice, -> (notice) { joins(:responses).merge(Response.where(notice: notice)) } Response::STATUSES.each do |status| @@ -22,7 +20,6 @@ class User < ActiveRecord::Base WHERE A.status is null} joins(SQL) } - scope :order_by_gid, -> {order(generation_id: :desc)} scope :order_by_responsed_at, -> {order('responses.created_at ASC')} scope :order_by_read_at, -> {order('read_activity_marks.created_at DESC')} diff --git a/app/views/admin/messages/show.html.erb b/app/views/admin/messages/show.html.erb index 3ed3ba6..33880f5 100644 --- a/app/views/admin/messages/show.html.erb +++ b/app/views/admin/messages/show.html.erb @@ -21,7 +21,7 @@ - <% @message.users.order_by_gid.each do |user| %> + <% @message.users.generation_sorted_desc.each do |user| %> <%= pretty_generation_id(user.generation_id) %> <%= user.username %> diff --git a/spec/unit/models/user_spec.rb b/spec/unit/models/user_spec.rb index 5c868e5..eb9ee74 100644 --- a/spec/unit/models/user_spec.rb +++ b/spec/unit/models/user_spec.rb @@ -25,8 +25,6 @@ expect(user.phone_number).to eq("01012341234") expect(user.username).to eq("user name") expect(user.email).to eq("test @ test.com") - - end describe "#responsed_to?" do @@ -41,34 +39,31 @@ end end - - describe "#response_status" do context "user didn't response to notice" do - it "should return satus, users responsed to notice" do + it "should return status, users responsed to notice" do user = FactoryGirl.create(:user) notice = FactoryGirl.create(:notice) expect(user.response_status(notice)).to eq("not") - end end + context "user responsed 'go' to notice" do it "should return satus, users responsed to notice" do user = FactoryGirl.create(:user) notice = FactoryGirl.create(:notice) response = Response.create!(user: user, notice: notice, status: "go") expect(user.response_status(notice)).to eq("go") - end end + context "user responsed 'go' to notice but he/she had to be 'wait'" do it "should return satus, users responsed to notice" do user = FactoryGirl.create(:user) notice = FactoryGirl.create(:notice) response = Response.create!(user: user, notice: notice, status: "wait") expect(user.response_status(notice)).to eq("wait") - end end end @@ -100,4 +95,85 @@ end end + describe "#generation_sorted_desc" do + it "should sort generation descending" do + user1 = FactoryGirl.create(:user, generation_id: 1) + user2 = FactoryGirl.create(:user, generation_id: 3) + user3 = FactoryGirl.create(:user, generation_id: 2) + + expect(User.generation_sorted_desc).to eq([user2, user3, user1]) + end + end + + describe "#responsed_to_notice" do + it "should fetch who responsed to notice" do + notice1 = FactoryGirl.create(:notice) + notice2 = FactoryGirl.create(:notice) + + user = FactoryGirl.create(:user) + Response.create!(user: user, notice: notice1, status: "yes") + + expect(User.responsed_to_notice(notice1)).to contain_exactly(user) + expect(User.responsed_to_notice(notice2)).to be_empty + end + end + + describe "#responsed_[Response::STATUSES]" do + it "should fetch who responsed to notice with given status" do + notice1 = FactoryGirl.create(:notice) + user1 = FactoryGirl.create(:user) + user2 = FactoryGirl.create(:user) + user3 = FactoryGirl.create(:user) + + Response.create!(user: user1, notice: notice1, status: "yes") + Response.create!(user: user2, notice: notice1, status: "no") + Response.create!(user: user3, notice: notice1, status: "yes") + + expect(User.responsed_yes(notice1)).to contain_exactly(user1, user3) + expect(User.responsed_no(notice1)).to contain_exactly(user2) + end + end + + describe "#responsed_not_to_notice" do + it "should fetch who did not response to notice" do + notice1 = FactoryGirl.create(:notice) + notice2 = FactoryGirl.create(:notice) + + user = FactoryGirl.create(:user) + Response.create!(user: user, notice: notice1, status: "yes") + + expect(User.responsed_not_to_notice(notice1)).to be_empty + expect(User.responsed_not_to_notice(notice2)).to contain_exactly(user) + end + end + + describe "#order_by_responsed_at" do + it "should sort responsed_at ascending" do + notice1 = FactoryGirl.create(:notice) + user1 = FactoryGirl.create(:user) + user2 = FactoryGirl.create(:user) + user3 = FactoryGirl.create(:user) + + Response.create!(user: user1, notice: notice1, status: "yes") + Response.create!(user: user3, notice: notice1, status: "yes") + Response.create!(user: user2, notice: notice1, status: "yes") + + expect(User.responsed_yes(notice1).order_by_responsed_at).to eq([user1, user3, user2]) + end + end + + describe "#order_by_read_at" do + it "should sort read_at descending" do + notice1 = FactoryGirl.create(:notice) + user1 = FactoryGirl.create(:user) + user2 = FactoryGirl.create(:user) + user3 = FactoryGirl.create(:user) + + user1.read!(notice1) + user3.read!(notice1) + user2.read!(notice1) + + expect(notice1.readers.order_by_read_at).to eq([user2, user3, user1]) + end + end end \ No newline at end of file