Skip to content

Commit

Permalink
Added support for Rails 6.1. Dropped support for Rails 5.0 and 5.1.
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-alexandrov committed Dec 25, 2020
1 parent 936eeb3 commit ef7151c
Show file tree
Hide file tree
Showing 31 changed files with 94 additions and 101 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Style/TrailingCommaInArrayLiteral:
Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: no_comma

Layout/AlignParameters:
Layout/ParameterAlignment:
EnforcedStyle: with_first_parameter

# See https://github.com/rubocop-hq/rubocop/issues/4222
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ local_gemfile = "#{File.dirname(__FILE__)}/Gemfile.local"
if File.exist?(local_gemfile)
eval(File.read(local_gemfile)) # rubocop:disable Lint/Eval
else
gem 'activerecord', '~> 5.2.4'
gem 'activerecord', '>= 5.2.4', '< 6.2'
end
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ PLATFORMS
ruby

DEPENDENCIES
activerecord (~> 5.2.4)
activerecord (>= 5.2.4, < 6.2)
ammeter (>= 1.1)
bundler (>= 1.3)
database_cleaner (>= 1.7)
Expand Down
6 changes: 0 additions & 6 deletions gemfiles/rails5.gemfile

This file was deleted.

2 changes: 1 addition & 1 deletion gemfiles/rails52.gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source "https://rubygems.org"

gem 'rails', '~>5.2'
gem 'rails', '~> 5.2'
gem 'activerecord', '~> 5.2.4.3'

gemspec path: '..'
4 changes: 2 additions & 2 deletions gemfiles/rails6.gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source "https://rubygems.org"

gem 'rails', '~>6.0'
gem 'activerecord', '~> 6.0'
gem 'rails', '~> 6.0', ' < 6.1'
gem 'activerecord', '~> 6.0', '< 6.1'

gemspec path: '..'
2 changes: 1 addition & 1 deletion gemfiles/rails61.gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source "https://rubygems.org"

gem 'rails', '~>6.1'
gem 'rails', '~> 6.1'
gem 'activerecord', '~> 6.1'

gemspec path: '..'
4 changes: 2 additions & 2 deletions lib/metka/generic_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def call(value)
gsub_quote_pattern!(tag_list, value, double_quote_pattern)
gsub_quote_pattern!(tag_list, value, single_quote_pattern)

tag_list.merge value.split(Regexp.new delimiter).map(&:strip).reject(&:empty?)
tag_list.merge value.split(Regexp.new(delimiter)).map(&:strip).reject(&:empty?)
when Enumerable
tag_list.merge value.reject(&:empty?)
end
Expand All @@ -46,7 +46,7 @@ def delimiter
end

def single_quote_pattern
@single_quote_pattern[delimiter] ||= /(\A|#{delimiter})\s*'(.*?)'\s*(?=#{delimiter}\s*|\z)/
@single_quote_pattern[delimiter] ||= /(\A|#{delimiter})\s*'(.*?)'\s*(?=#{delimiter}\s*|\z)/
end

def double_quote_pattern
Expand Down
10 changes: 5 additions & 5 deletions lib/metka/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ def included(base)

base.class_eval do
columns.each do |column|
scope "with_all_#{column}", ->(tags) { tagged_with(tags, on: [ column ]) }
scope "with_any_#{column}", ->(tags) { tagged_with(tags, on: [ column ], any: true) }
scope "without_all_#{column}", ->(tags) { tagged_with(tags, on: [ column ], exclude: true) }
scope "without_any_#{column}", ->(tags) { tagged_with(tags, on: [ column ], any: true, exclude: true) }
scope "with_all_#{column}", ->(tags) { tagged_with(tags, on: [column]) }
scope "with_any_#{column}", ->(tags) { tagged_with(tags, on: [column], any: true) }
scope "without_all_#{column}", ->(tags) { tagged_with(tags, on: [column], exclude: true) }
scope "without_any_#{column}", ->(tags) { tagged_with(tags, on: [column], any: true, exclude: true) }
end

unless respond_to?(:tagged_with)
Expand All @@ -66,7 +66,7 @@ def included(base)
prepared_unnest = columns.map { |column| "#{table_name}.#{column}" }.join(' || ')
subquery = all.select("UNNEST(#{prepared_unnest}) AS tag_name")

unscoped.from(subquery).group(:tag_name).pluck(:tag_name, 'COUNT(*) AS taggings_count')
unscoped.from(subquery).group(:tag_name).pluck(:tag_name, Arel.sql('COUNT(*) AS taggings_count'))
end

columns.each do |column|
Expand Down
12 changes: 7 additions & 5 deletions lib/metka/query_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ class QueryBuilder
def call(model, columns, tags, options)
strategy = options_to_strategy(options)

query = join(options[:join_operator]) do
query = join(options[:join_operator]) {
columns.map do |column|
build_query(strategy, model, column, tags)
end
end
}

if options[:exclude].present?
Arel::Nodes::Not.new(query)
Expand Down Expand Up @@ -43,11 +43,13 @@ def join(operator, &block)
end
end

# @param nodes [Array<Arel::Node>, Arel::Node]
# @return [Arel::Node]
# @param nodes [Array<Arel::Nodes::Node>, Arel::Nodes::Node]
# @return [Arel::Nodes::Node]
def join_or(nodes)
node_base_klass = defined?(::Arel::Nodes::Node) ? ::Arel::Nodes::Node : ::Arel::Node

case nodes
when ::Arel::Node
when node_base_klass
nodes
when Array
l, *r = nodes
Expand Down
1 change: 1 addition & 0 deletions lib/metka/query_builder/base_query.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require 'singleton'

module Metka
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/models/materialized_view_multitag_post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# This class use materialized view strategy with aggregation by multiple tagged columns
# You can find out more here: lib/generators/metka/strategies/materialized_view/materialized_view_generator.rb
class MaterializedViewMultitagPost < ActiveRecord::Base
include Metka::Model(columns: %w(tags categories))
include Metka::Model(columns: %w[tags categories])

belongs_to :user
end
2 changes: 1 addition & 1 deletion spec/dummy/app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# This class use ActiveRecord Strategy
class Post < ActiveRecord::Base
include Metka::Model(columns: %w(tags categories))
include Metka::Model(columns: %w[tags categories])

belongs_to :user
end
1 change: 0 additions & 1 deletion spec/dummy/app/models/tagged_materialized_view_post.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# frozen_string_literal: true

class TaggedMaterializedViewPost < ActiveRecord::Base

end
1 change: 0 additions & 1 deletion spec/dummy/app/models/tagged_view_post.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# frozen_string_literal: true

class TaggedViewPost < ActiveRecord::Base

end
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# frozen_string_literal: true

class TaggedWithTagsAndCategoriesMaterializedViewMultitagPost < ActiveRecord::Base

end
2 changes: 1 addition & 1 deletion spec/dummy/config/environments/test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

Rails.application.configure do
config.logger = ENV['LOG'] ? Logger.new(STDOUT) : nil
config.logger = ENV['LOG'] ? Logger.new($stdout) : nil
config.active_record.dump_schema_after_migration = false
end
6 changes: 3 additions & 3 deletions spec/dummy/db/migrate/02_create_posts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
class CreatePosts < ActiveRecord::Migration[5.0]
def change
create_table :posts do |t|
t.string :title
t.string :title
t.integer :user_id, null: false
t.string :tags, array: true
t.string :categories, array: true
t.string :tags, array: true
t.string :categories, array: true
t.timestamps
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/dummy/db/migrate/03_create_view_posts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
class CreateViewPosts < ActiveRecord::Migration[5.0]
def change
create_table :view_posts do |t|
t.string :title
t.string :title
t.integer :user_id, null: false
t.string :tags, array: true
t.string :tags, array: true
t.timestamps
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/dummy/db/migrate/04_create_materialized_view_posts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
class CreateMaterializedViewPosts < ActiveRecord::Migration[5.0]
def change
create_table :materialized_view_posts do |t|
t.string :title
t.string :title
t.integer :user_id, null: false
t.string :tags, array: true
t.string :tags, array: true

t.timestamps
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
class CreateMaterializedViewMultitagPosts < ActiveRecord::Migration[5.0]
def change
create_table :materialized_view_multitag_posts do |t|
t.string :title
t.string :title
t.integer :user_id, null: false
t.string :tags, array: true
t.string :categories, array: true
t.string :tags, array: true
t.string :categories, array: true
t.timestamps
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require 'spec_helper'
require 'generators/metka/strategies/materialized_view/materialized_view_generator.rb'
require 'generators/metka/strategies/materialized_view/materialized_view_generator'

RSpec.describe Metka::Generators::Strategies::MaterializedViewGenerator, type: :generator do
destination File.expand_path('../../tmp', __dir__)
Expand Down Expand Up @@ -52,4 +52,3 @@
end
end
end

1 change: 0 additions & 1 deletion spec/metka/generic_parser_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding: utf-8
# frozen_string_literal: true

require 'spec_helper'
Expand Down
4 changes: 2 additions & 2 deletions spec/metka/model/tagged_without_any_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
RSpec.describe Metka::Model, :db do
let!(:user) { User.create(name: Faker::Name.name) }

let!(:first_post) { Post.create(user_id: user.id, tags: ['ruby', 'elixir', 'crystal'], categories: ['ruby', 'programming']) }
let!(:first_post) { Post.create(user_id: user.id, tags: ['ruby', 'elixir', 'crystal'], categories: ['ruby', 'programming']) }
let!(:second_post) { Post.create(user_id: user.id, tags: ['ruby', 'rails', 'react'], categories: ['programming', 'backend']) }
let!(:third_post) { Post.create(user_id: user.id, tags: ['php', 'yii2', 'angular'], categories: []) }
let!(:third_post) { Post.create(user_id: user.id, tags: ['php', 'yii2', 'angular'], categories: []) }

context 'when use default join operator' do
it 'should return collection without tag ruby' do
Expand Down
10 changes: 5 additions & 5 deletions spec/metka/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
require 'spec_helper'

RSpec.describe Metka::Model, :db do
let!(:tag_list) { 'ruby, rails, crystal' }
let!(:tag_list) { 'ruby, rails, crystal' }
let!(:category_list) { 'programming, backend, frontend' }

let!(:user) { User.create(name: Faker::Name.name) }
let!(:user1) { User.create!(name: Faker::Name.name, tags: %w[developer senior]) }
let!(:user2) { User.create!(name: Faker::Name.name, tags: ['junior']) }
let!(:post) { Post.new(user_id: user.id)}
let!(:user) { User.create(name: Faker::Name.name) }
let!(:user1) { User.create!(name: Faker::Name.name, tags: %w[developer senior]) }
let!(:user2) { User.create!(name: Faker::Name.name, tags: ['junior']) }
let!(:post) { Post.new(user_id: user.id) }
let!(:post_two) { Post.new(user_id: user.id)}

before do
Expand Down
10 changes: 5 additions & 5 deletions spec/metka/post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
require 'spec_helper'

RSpec.describe Metka::Post, :model do
let!(:tag1) { 'tag1' }
let!(:tag2) { 'tag2' }
let!(:category1) { 'category1' }
let!(:category2) { 'category2' }
let!(:tag1) { 'tag1' }
let!(:tag2) { 'tag2' }
let!(:category1) { 'category1' }
let!(:category2) { 'category2' }
let!(:shared_tag) { 'sharedtag' }
let!(:unused_tag) { 'tag3' }
let!(:user) { User.create(name: Faker::Name.name) }
let!(:user) { User.create(name: Faker::Name.name) }

let(:tagged_model) { Post }

Expand Down
22 changes: 11 additions & 11 deletions spec/metka/tagged_materialized_view_post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
require 'spec_helper'

RSpec.describe Metka::TaggedMaterializedViewPost, :model do
let!(:tag1) { 'tag1' }
let!(:tag2) { 'tag2' }
let!(:tag1) { 'tag1' }
let!(:tag2) { 'tag2' }
let!(:unused_tag) { 'tag3' }
let!(:user) { User.create(name: Faker::Name.name) }
let!(:user) { User.create(name: Faker::Name.name) }

let(:tagged_model) { TaggedMaterializedViewPost }

Expand All @@ -32,32 +32,32 @@
end

it 'increases the counter on post with tag addition' do
expect{ MaterializedViewPost.create(user_id: user.id, tag_list: tag2) }
.to change{ tagged_model.find_by(tag_name: tag2).taggings_count }
expect { MaterializedViewPost.create(user_id: user.id, tag_list: tag2) }
.to change { tagged_model.find_by(tag_name: tag2).taggings_count }
.by(1)
end

it 'decreases the counter on post with tag removal' do
expect{ materialized_view_post_1.delete }
.to change{ tagged_model.find_by(tag_name: tag1).taggings_count }
expect { materialized_view_post_1.delete }
.to change { tagged_model.find_by(tag_name: tag1).taggings_count }
.by(-1)
end

it 'increases the counter on post tags expansion via update' do
expect { materialized_view_post_1.update(tag_list: [tag1, tag2]) }
.to change{ tagged_model.find_by(tag_name: tag2).taggings_count }
.to change { tagged_model.find_by(tag_name: tag2).taggings_count }
.by(1)
end

it 'decreases the counter on post tags narrowing via update' do
expect { materialized_view_post_2.update(tag_list: tag2) }
.to change{ tagged_model.find_by(tag_name: tag1).taggings_count }
.to change { tagged_model.find_by(tag_name: tag1).taggings_count }
.by(-1)
end

it 'decreases the counter on post tags nullify' do
expect{ materialized_view_post_1.update(tag_list: nil) }
.to change{ tagged_model.find_by(tag_name: tag1).taggings_count }
expect { materialized_view_post_1.update(tag_list: nil) }
.to change { tagged_model.find_by(tag_name: tag1).taggings_count }
.by(-1)
end
end
Expand Down
Loading

0 comments on commit ef7151c

Please sign in to comment.