Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/issue 333 #378

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions app/models/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@ class Response < ActiveRecord::Base
belongs_to :notice
belongs_to :user

scope :time, -> (notice) { find_by_notice_id(notice.id).created_at.localtime.strftime("%Y-%m-%d %T") }
validates :status, presence: { message: "회답을 선택해주십시오." },
inclusion: { in: STATUSES, message: "올바르지 않은 회답입니다." }

scope :responsed_to_go, -> (notice) { notice.responses.where(status: "go") }

def self.time (notice)
response = find_by_notice_id(notice.id)

if response
return response.created_at.localtime.strftime("%Y-%m-%d %T")
else
return ""
end
end

def responsed_at
created_at.localtime.strftime("%Y-%m-%d %T")
end
validates :status, presence: { message: "회답을 선택해주십시오." },
inclusion: { in: STATUSES, message: "올바르지 않은 회답입니다." }

end
5 changes: 2 additions & 3 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ class Tag < ActiveRecord::Base
has_many :taggings
has_many :users, through: :taggings, source: :user

scope :fetch_list_by_tag_name, -> (tag_name){
scope :fetch_list_by_tag_name, -> (tag_name) {
tag_arel = Tag.arel_table
where(tag_arel[:tag_name].matches("%#{tag_name}%"))
}

end
end
7 changes: 2 additions & 5 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,17 @@ 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|
scope "responsed_#{status}", -> (notice) { responsed_to_notice(notice).merge(Response.where(status: status)) }
end
scope :responsed_not_to_notice, -> (notice) {
SQL = %{LEFT OUTER JOIN (SELECT * FROM responses WHERE responses.notice_id = #{notice.id} ) A
sql = %{LEFT OUTER JOIN (SELECT * FROM responses WHERE responses.notice_id = #{notice.id} ) A
ON users.id = A.user_id
WHERE A.status is null}
joins(SQL) }
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
11 changes: 11 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@

config.include ShowMeTheCookies, :type => :feature

config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end

config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
Expand Down
29 changes: 29 additions & 0 deletions spec/unit/models/notice_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
require "rails_helper"

RSpec.describe Notice, :type => :model do
describe "#[NOTICE_TYPES]_notice?" do
it "should return whether notice is given type" do
given_type = Notice::NOTICE_TYPES.first
notice = FactoryGirl.create(:notice, notice_type: given_type)

expect(notice.send("#{given_type}_notice?")).to eq(true)
Notice::NOTICE_TYPES[1..-1].each do |type|
expect(notice.send("#{type}_notice?")).to eq(false)
end
end
end

describe "#[Response::STATUSES]_responses" do
it "should return responses that is given status" do
user = FactoryGirl.create(:user)
notice = FactoryGirl.create(:notice)

given_status = Response::STATUSES.first
expect(notice.send("#{given_status}_responses").empty?).to eq(true)

response = Response.create!(user: user, notice: notice, status: given_status)
expect(notice.send("#{given_status}_responses")).to match_array([response])

Response::STATUSES[1..-1].each do |status|
expect(notice.send("#{status}_responses").empty?).to eq(true)
end
end
end

describe "#save with make_redirectable_url!" do
it "should prepend protocol if not exist" do
notice = FactoryGirl.create(:notice, link: "www.google.com", shortenURL: "www.google.com")
Expand Down
38 changes: 37 additions & 1 deletion spec/unit/models/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,47 @@
it "should check validation" do
expect(Response.new.save).to eq(false)
expect(Response.new(status: "false", notice: FactoryGirl.create(:notice)).save).to eq(false)

expect(Response.new(status: "yes", notice: FactoryGirl.create(:notice)).save).to eq(true)
expect(Response.new(status: "maybe", notice: FactoryGirl.create(:notice)).save).to eq(true)
expect(Response.new(status: "no", notice: FactoryGirl.create(:notice)).save).to eq(true)
expect(Response.new(status: "go", notice: FactoryGirl.create(:notice)).save).to eq(true)
expect(Response.new(status: "wait", notice: FactoryGirl.create(:notice)).save).to eq(true)
end

describe "#responsed_to_go" do
it "should return responsed to go" do
user1 = FactoryGirl.create(:user)
user2 = FactoryGirl.create(:user)
notice = FactoryGirl.create(:notice)

response1 = Response.create!(user: user1, notice: notice, status: "maybe")
expect(Response.responsed_to_go(notice)).to be_empty

response2 = Response.create!(user: user2, notice: notice, status: "go")
expect(Response.responsed_to_go(notice)).to contain_exactly(response2)
end
end

describe "#responsed_at" do
it "should return created_at with localtime manner" do
user = FactoryGirl.create(:user)
notice = FactoryGirl.create(:notice)

response = Response.create!(user: user, notice: notice, status: "maybe", created_at: Date.new(2014, 10, 30))
expect(response.responsed_at).to eq("2014-10-30 00:00:00")
end
end

describe ".time" do
it "should return responsed time to given notice" do
user = FactoryGirl.create(:user)
notice = FactoryGirl.create(:notice)

expect(user.responses.time(notice)).to eq("")

response = Response.create!(user: user, notice: notice, status: "maybe", created_at: Date.new(2014, 10, 30))
expect(user.responses.time(notice)).to eq("2014-10-30 00:00:00")
end
end
end
14 changes: 14 additions & 0 deletions spec/unit/models/tag_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "rails_helper"

RSpec.describe Tag, :type => :model do
describe "#fetch_list_by_tag_name" do
it "should fetch tag list where like name" do
tag1 = Tag.create!(tag_name: "Hello")
tag2 = Tag.create!(tag_name: "World")
tag3 = Tag.create!(tag_name: "Yellow")

expect(Tag.fetch_list_by_tag_name("Worl")).to contain_exactly(tag2)
expect(Tag.fetch_list_by_tag_name("llo")).to contain_exactly(tag1, tag3)
end
end
end
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