diff --git a/app/models/response.rb b/app/models/response.rb
index c51f7d8..5a4b54f 100644
--- a/app/models/response.rb
+++ b/app/models/response.rb
@@ -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
\ No newline at end of file
diff --git a/app/models/tag.rb b/app/models/tag.rb
index 722b68d..d0ccedc 100644
--- a/app/models/tag.rb
+++ b/app/models/tag.rb
@@ -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
\ No newline at end of file
diff --git a/app/models/user.rb b/app/models/user.rb
index 019c14d..77b7a01 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -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')}
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/rails_helper.rb b/spec/rails_helper.rb
index 70813ad..b03ea97 100644
--- a/spec/rails_helper.rb
+++ b/spec/rails_helper.rb
@@ -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.
diff --git a/spec/unit/models/notice_spec.rb b/spec/unit/models/notice_spec.rb
index bb7cf02..d3b42bb 100644
--- a/spec/unit/models/notice_spec.rb
+++ b/spec/unit/models/notice_spec.rb
@@ -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")
diff --git a/spec/unit/models/response_spec.rb b/spec/unit/models/response_spec.rb
index de41aa8..5a1097c 100644
--- a/spec/unit/models/response_spec.rb
+++ b/spec/unit/models/response_spec.rb
@@ -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
\ No newline at end of file
diff --git a/spec/unit/models/tag_spec.rb b/spec/unit/models/tag_spec.rb
new file mode 100644
index 0000000..24e5a49
--- /dev/null
+++ b/spec/unit/models/tag_spec.rb
@@ -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
\ No newline at end of file
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