-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from wbotelhos/adding-table-config
Adding table config
- Loading branch information
Showing
26 changed files
with
343 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.byebug_history | ||
*.gem | ||
/config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rating | ||
module Config | ||
module_function | ||
|
||
def config | ||
@config ||= begin | ||
file_path = File.expand_path('config/rating.yml') | ||
|
||
return {} unless File.exist?(file_path) | ||
|
||
YAML.safe_load(File.read(file_path))['rating'] | ||
end | ||
end | ||
|
||
def rate_table | ||
@rate_table ||= config[__method__.to_s] || 'rating_rates' | ||
end | ||
|
||
def rating_table | ||
@rating_table ||= config[__method__.to_s] || 'rating_ratings' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Rating::Config, '.rate_table' do | ||
context 'when rating.yml does not exist' do | ||
it { expect(subject.rate_table).to eq 'rating_rates' } | ||
end if ENV['CONFIG_ENABLED'] != 'true' | ||
|
||
context 'when rating.yml exists' do | ||
it { expect(subject.rate_table).to eq 'reviews' } | ||
end if ENV['CONFIG_ENABLED'] == 'true' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Rating::Config, '.rating_table' do | ||
context 'when rating.yml does not exist' do | ||
it { expect(subject.rating_table).to eq 'rating_ratings' } | ||
end if ENV['CONFIG_ENABLED'] != 'true' | ||
|
||
context 'when rating.yml exists' do | ||
it { expect(subject.rating_table).to eq 'review_ratings' } | ||
end if ENV['CONFIG_ENABLED'] == 'true' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :comment | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Rating::Extension, '.rating_warm_up' do | ||
context 'when scoping is nil' do | ||
context 'when update is made' do | ||
let!(:record) { create :comment } | ||
let!(:rating) { ::Rating::Rating.find_by resource: record } | ||
|
||
it 'creates the cache' do | ||
record.rating_warm_up scoping: nil | ||
|
||
expect(::Rating::Rating.all).to eq [rating] | ||
end | ||
|
||
it 'returns the cached object' do | ||
expect(record.rating_warm_up).to eq rating | ||
end | ||
end | ||
|
||
context 'when record does not exist' do | ||
let!(:record) { create :comment } | ||
|
||
before { ::Rating::Rating.destroy_all } | ||
|
||
it 'creates the cache' do | ||
record.rating_warm_up scoping: nil | ||
|
||
expect(::Rating::Rating.all.map(&:resource)).to eq [record] | ||
end | ||
|
||
it 'returns the cached object' do | ||
expect(record.rating_warm_up).to eq ::Rating::Rating.last | ||
end | ||
end | ||
end | ||
|
||
context 'when scoping is not nil' do | ||
context 'when update is made' do | ||
let!(:category_1) { create :category } | ||
let!(:category_2) { create :category } | ||
let!(:record) { create :article, categories: [category_1, category_2] } | ||
|
||
it 'creates the cache' do | ||
record.rating_warm_up scoping: :categories | ||
|
||
ratings = ::Rating::Rating.all | ||
|
||
expect(ratings.map(&:scopeable)).to match_array [category_1, category_2] | ||
expect(ratings.map(&:resource)).to match_array [record, record] | ||
end | ||
|
||
it 'returns the cached objects' do | ||
expect(record.rating_warm_up(scoping: :categories)).to eq ::Rating::Rating.all | ||
end | ||
end | ||
|
||
context 'when record does not exist' do | ||
let!(:category_1) { create :category } | ||
let!(:category_2) { create :category } | ||
let!(:record) { create :article, categories: [category_1, category_2] } | ||
|
||
before { ::Rating::Rating.destroy_all } | ||
|
||
it 'creates the cache' do | ||
record.rating_warm_up scoping: :categories | ||
|
||
ratings = ::Rating::Rating.all | ||
|
||
expect(ratings.map(&:scopeable)).to match_array [category_1, category_2] | ||
expect(ratings.map(&:resource)).to match_array [record, record] | ||
end | ||
|
||
it 'returns the cached objects' do | ||
expect(record.rating_warm_up(scoping: :categories)).to eq ::Rating::Rating.all | ||
end | ||
end | ||
|
||
context 'when a non existent scoping is given' do | ||
let!(:record) { create :article } | ||
|
||
it 'returns an empty array' do | ||
expect(record.rating_warm_up(scoping: :missing)).to eq [] | ||
end | ||
end | ||
|
||
context 'when scoping is given inside array' do | ||
let!(:category) { create :category } | ||
let!(:record) { create :article, categories: [category] } | ||
|
||
it 'returns the cache' do | ||
expect(record.rating_warm_up(scoping: [:categories])).to eq ::Rating::Rating.all | ||
end | ||
end | ||
|
||
context 'when scoping is given inside multiple arrays' do | ||
let!(:category) { create :category } | ||
let!(:record) { create :article, categories: [category] } | ||
|
||
it 'returns the cache' do | ||
expect(record.rating_warm_up(scoping: [[:categories]])).to eq ::Rating::Rating.all | ||
end | ||
end | ||
|
||
context 'when scoping is given with nil value together' do | ||
let!(:category) { create :category } | ||
let!(:record) { create :article, categories: [category] } | ||
|
||
it 'returns the cache' do | ||
expect(record.rating_warm_up(scoping: [[:categories, nil], nil])).to eq ::Rating::Rating.all | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.