Skip to content

Commit

Permalink
test added: tag
Browse files Browse the repository at this point in the history
  • Loading branch information
angdev committed Oct 30, 2014
1 parent 4d3e937 commit 55a242d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
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
4 changes: 2 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class User < ActiveRecord::Base
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_responsed_at, -> {order('responses.created_at ASC')}
scope :order_by_read_at, -> {order('read_activity_marks.created_at DESC')}
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
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

0 comments on commit 55a242d

Please sign in to comment.