Skip to content

Commit

Permalink
added user test: scope
Browse files Browse the repository at this point in the history
  • Loading branch information
angdev committed Oct 30, 2014
1 parent fae8eb4 commit cc674f8
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 12 deletions.
3 changes: 0 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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')}

Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/messages/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</tr>
</thead>
<tbody>
<% @message.users.order_by_gid.each do |user| %>
<% @message.users.generation_sorted_desc.each do |user| %>
<tr>
<td class="text-right"><%= pretty_generation_id(user.generation_id) %></td>
<td><%= user.username %></td>
Expand Down
92 changes: 84 additions & 8 deletions spec/unit/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

0 comments on commit cc674f8

Please sign in to comment.