metka 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 39ab36dcfd96ca5a0773e6c51f6746df34516397f73d225e2eb2777ed33c2874
4
+ data.tar.gz: 614c80c9f813166657f33db212284b98fd7f85fd0d1cb872150c4f6a3608d559
5
+ SHA512:
6
+ metadata.gz: cb6ad90572fcae32a8208cad5181c7c8f1f0bdca0522c8ee0a25768089bd6eaf1e2d9903c704e72510943c0a1483267277b09466358bacc6ee98154e9209ecdb
7
+ data.tar.gz: ee0ef24ceb8d6b39fe771fa588072572803293c9f1307f5df08d4e424ea158848c12aa65fad0936364dfb3f7e27a8991d2325bc75af879d297e6532f74effcea
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,34 @@
1
+ inherit_gem:
2
+ standard: config/base.yml
3
+
4
+ AllCops:
5
+ Exclude:
6
+ - 'bin/*'
7
+ - 'tmp/**/*'
8
+ - 'docs/**/*'
9
+ - 'Gemfile'
10
+ - 'vendor/**/*'
11
+ - 'gemfiles/**/*'
12
+ - '**/*.md'
13
+ DisplayCopNames: true
14
+ TargetRubyVersion: 2.5
15
+
16
+ Standard/SemanticBlocks:
17
+ Enabled: false
18
+
19
+ Style/FrozenStringLiteralComment:
20
+ Enabled: true
21
+
22
+ Style/TrailingCommaInArrayLiteral:
23
+ EnforcedStyleForMultiline: no_comma
24
+
25
+ Style/TrailingCommaInHashLiteral:
26
+ EnforcedStyleForMultiline: no_comma
27
+
28
+ Layout/AlignParameters:
29
+ EnforcedStyle: with_first_parameter
30
+
31
+ # See https://github.com/rubocop-hq/rubocop/issues/4222
32
+ Lint/AmbiguousBlockAssociation:
33
+ Exclude:
34
+ - 'spec/**/*'
@@ -0,0 +1,22 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+
5
+ dist: trusty
6
+ sudo: false
7
+
8
+ notifications:
9
+ email: false
10
+
11
+ addons:
12
+ postgresql: '9.6'
13
+
14
+ env:
15
+ global:
16
+ - METKA_DB_USER=postgres
17
+ - METKA_DB_NAME=metka
18
+
19
+ before_script:
20
+ - gem update --system
21
+ - bundle exec rake dummy:db:create
22
+ - RAILS_ENV=test bundle exec rake dummy:db:create dummy:db:migrate
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at igor.alexandrov@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in metka.gemspec
4
+ gemspec
5
+
6
+ local_gemfile = "#{File.dirname(__FILE__)}/Gemfile.local"
7
+
8
+ if File.exist?(local_gemfile)
9
+ eval(File.read(local_gemfile)) # rubocop:disable Lint/Eval
10
+ else
11
+ gem 'activerecord', '~> 5.1.1'
12
+ end
@@ -0,0 +1,183 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ metka (0.1.0)
5
+ dry-configurable
6
+ rails (>= 4.2)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actioncable (5.1.7)
12
+ actionpack (= 5.1.7)
13
+ nio4r (~> 2.0)
14
+ websocket-driver (~> 0.6.1)
15
+ actionmailer (5.1.7)
16
+ actionpack (= 5.1.7)
17
+ actionview (= 5.1.7)
18
+ activejob (= 5.1.7)
19
+ mail (~> 2.5, >= 2.5.4)
20
+ rails-dom-testing (~> 2.0)
21
+ actionpack (5.1.7)
22
+ actionview (= 5.1.7)
23
+ activesupport (= 5.1.7)
24
+ rack (~> 2.0)
25
+ rack-test (>= 0.6.3)
26
+ rails-dom-testing (~> 2.0)
27
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
28
+ actionview (5.1.7)
29
+ activesupport (= 5.1.7)
30
+ builder (~> 3.1)
31
+ erubi (~> 1.4)
32
+ rails-dom-testing (~> 2.0)
33
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
34
+ activejob (5.1.7)
35
+ activesupport (= 5.1.7)
36
+ globalid (>= 0.3.6)
37
+ activemodel (5.1.7)
38
+ activesupport (= 5.1.7)
39
+ activerecord (5.1.7)
40
+ activemodel (= 5.1.7)
41
+ activesupport (= 5.1.7)
42
+ arel (~> 8.0)
43
+ activesupport (5.1.7)
44
+ concurrent-ruby (~> 1.0, >= 1.0.2)
45
+ i18n (>= 0.7, < 2)
46
+ minitest (~> 5.1)
47
+ tzinfo (~> 1.1)
48
+ ammeter (1.1.4)
49
+ activesupport (>= 3.0)
50
+ railties (>= 3.0)
51
+ rspec-rails (>= 2.2)
52
+ arel (8.0.0)
53
+ ast (2.4.0)
54
+ builder (3.2.3)
55
+ concurrent-ruby (1.1.5)
56
+ crass (1.0.4)
57
+ diff-lcs (1.3)
58
+ dry-configurable (0.8.3)
59
+ concurrent-ruby (~> 1.0)
60
+ dry-core (~> 0.4, >= 0.4.7)
61
+ dry-core (0.4.9)
62
+ concurrent-ruby (~> 1.0)
63
+ erubi (1.8.0)
64
+ faker (2.1.2)
65
+ i18n (>= 0.8)
66
+ globalid (0.4.2)
67
+ activesupport (>= 4.2.0)
68
+ i18n (1.6.0)
69
+ concurrent-ruby (~> 1.0)
70
+ jaro_winkler (1.5.3)
71
+ loofah (2.2.3)
72
+ crass (~> 1.0.2)
73
+ nokogiri (>= 1.5.9)
74
+ mail (2.7.1)
75
+ mini_mime (>= 0.1.1)
76
+ method_source (0.9.2)
77
+ mini_mime (1.0.2)
78
+ mini_portile2 (2.4.0)
79
+ minitest (5.11.3)
80
+ nio4r (2.4.0)
81
+ nokogiri (1.10.4)
82
+ mini_portile2 (~> 2.4.0)
83
+ parallel (1.17.0)
84
+ parser (2.6.3.0)
85
+ ast (~> 2.4.0)
86
+ pg (1.1.4)
87
+ rack (2.0.7)
88
+ rack-test (1.1.0)
89
+ rack (>= 1.0, < 3)
90
+ rails (5.1.7)
91
+ actioncable (= 5.1.7)
92
+ actionmailer (= 5.1.7)
93
+ actionpack (= 5.1.7)
94
+ actionview (= 5.1.7)
95
+ activejob (= 5.1.7)
96
+ activemodel (= 5.1.7)
97
+ activerecord (= 5.1.7)
98
+ activesupport (= 5.1.7)
99
+ bundler (>= 1.3.0)
100
+ railties (= 5.1.7)
101
+ sprockets-rails (>= 2.0.0)
102
+ rails-dom-testing (2.0.3)
103
+ activesupport (>= 4.2.0)
104
+ nokogiri (>= 1.6)
105
+ rails-html-sanitizer (1.2.0)
106
+ loofah (~> 2.2, >= 2.2.2)
107
+ railties (5.1.7)
108
+ actionpack (= 5.1.7)
109
+ activesupport (= 5.1.7)
110
+ method_source
111
+ rake (>= 0.8.7)
112
+ thor (>= 0.18.1, < 2.0)
113
+ rainbow (3.0.0)
114
+ rake (12.3.3)
115
+ rspec (3.8.0)
116
+ rspec-core (~> 3.8.0)
117
+ rspec-expectations (~> 3.8.0)
118
+ rspec-mocks (~> 3.8.0)
119
+ rspec-core (3.8.2)
120
+ rspec-support (~> 3.8.0)
121
+ rspec-expectations (3.8.4)
122
+ diff-lcs (>= 1.2.0, < 2.0)
123
+ rspec-support (~> 3.8.0)
124
+ rspec-mocks (3.8.1)
125
+ diff-lcs (>= 1.2.0, < 2.0)
126
+ rspec-support (~> 3.8.0)
127
+ rspec-rails (3.8.2)
128
+ actionpack (>= 3.0)
129
+ activesupport (>= 3.0)
130
+ railties (>= 3.0)
131
+ rspec-core (~> 3.8.0)
132
+ rspec-expectations (~> 3.8.0)
133
+ rspec-mocks (~> 3.8.0)
134
+ rspec-support (~> 3.8.0)
135
+ rspec-support (3.8.2)
136
+ rubocop (0.72.0)
137
+ jaro_winkler (~> 1.5.1)
138
+ parallel (~> 1.10)
139
+ parser (>= 2.6)
140
+ rainbow (>= 2.2.2, < 4.0)
141
+ ruby-progressbar (~> 1.7)
142
+ unicode-display_width (>= 1.4.0, < 1.7)
143
+ rubocop-performance (1.4.1)
144
+ rubocop (>= 0.71.0)
145
+ ruby-progressbar (1.10.1)
146
+ sprockets (3.7.2)
147
+ concurrent-ruby (~> 1.0)
148
+ rack (> 1, < 3)
149
+ sprockets-rails (3.2.1)
150
+ actionpack (>= 4.0)
151
+ activesupport (>= 4.0)
152
+ sprockets (>= 3.0.0)
153
+ standard (0.1.2)
154
+ rubocop (~> 0.72.0)
155
+ rubocop-performance (~> 1.4.0)
156
+ thor (0.20.3)
157
+ thread_safe (0.3.6)
158
+ timecop (0.9.1)
159
+ tzinfo (1.2.5)
160
+ thread_safe (~> 0.1)
161
+ unicode-display_width (1.6.0)
162
+ websocket-driver (0.6.5)
163
+ websocket-extensions (>= 0.1.0)
164
+ websocket-extensions (0.1.4)
165
+
166
+ PLATFORMS
167
+ ruby
168
+
169
+ DEPENDENCIES
170
+ activerecord (~> 5.1.1)
171
+ ammeter
172
+ bundler
173
+ faker
174
+ metka!
175
+ pg
176
+ rake
177
+ rspec
178
+ rspec-rails
179
+ standard
180
+ timecop
181
+
182
+ BUNDLED WITH
183
+ 2.0.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Igor Alexandrov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,90 @@
1
+ [![Build Status](https://travis-ci.org/jetrockets/metka.svg?branch=master)](https://travis-ci.org/jetrockets/metka)
2
+ [![Open Source Helpers](https://www.codetriage.com/jetrockets/metka/badges/users.svg)](https://www.codetriage.com/jetrockets/metka)
3
+
4
+ # Metka
5
+
6
+ Rails gem to manage tags with PostgreSQL array columns.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'metka'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install metka
23
+
24
+ ## Tag objects
25
+
26
+ ```ruby
27
+ class Post < ActiveRecord::Base
28
+ include Metka::Model
29
+
30
+ end
31
+
32
+ @post = Post.new(title: 'Migrate tags in Rails to PostgreSQL')
33
+ @post.tags = ['ruby', 'postgres', 'rails']
34
+ @post.save
35
+ ```
36
+
37
+ ## Find tagged objects
38
+
39
+ ```ruby
40
+ Post.tagged_with('ruby')
41
+ => [#<Post id: 1, title: 'Migrate tags in Rails to PostgreSQL', tags: ['ruby', 'postgres', 'rails']
42
+
43
+ Post.tagged_with('ruby, crystal')
44
+ => nil
45
+ ```
46
+
47
+ In example above you will get records that are tagged with `ruby` and `crystal`. To get records that are tagged with any of these tags use `any` option.
48
+
49
+ ```ruby
50
+ Post.tagged_with('ruby, crystal', any: true)
51
+ => [#<Post id: 1, title: 'Migrate tags in Rails to PostgreSQL', tags: ['ruby', 'postgres', 'rails']
52
+ ```
53
+
54
+ ## Tag Cloud Strategies
55
+
56
+ There are several strategies to get tag statistics
57
+
58
+ ### View Strategy
59
+
60
+ The easiest way to implement but the most slow on SELECT.
61
+
62
+ ```bash
63
+ rails g metka:strategies:view --source-table-name=NAME_OF_TABLE_WITH_TAGS
64
+ ```
65
+
66
+ ### Materialized View Strategy
67
+
68
+ TBD
69
+
70
+ ### Table Strategy with Triggers
71
+
72
+ TBD
73
+
74
+ ## Development
75
+
76
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
77
+
78
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
79
+
80
+ ## Contributing
81
+
82
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/metka. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
83
+
84
+ ## License
85
+
86
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
87
+
88
+ ## Code of Conduct
89
+
90
+ Everyone interacting in the Metka project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/metka/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ namespace :dummy do
9
+ require_relative "spec/dummy/config/application"
10
+ Dummy::Application.load_tasks
11
+ end
12
+
13
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "metka"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,9 @@
1
+ #!/bin/sh
2
+
3
+ set -e
4
+
5
+ gem install bundler --conservative
6
+ bundle check || bundle install
7
+
8
+ RAILS_ENV=test bundle exec rake dummy:db:create
9
+ RAILS_ENV=test bundle exec rake dummy:db:schema:load
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ class <%= @migration_class_name %> < ActiveRecord::Migration<%= ActiveRecord::VERSION::MAJOR < 5 ? '' : '[5.0]' %>
4
+ def up
5
+ execute <<-SQL
6
+ CREATE OR REPLACE VIEW <%= view_name %> AS
7
+
8
+ SELECT UNNEST
9
+ ( <%= source_column_name %> ) AS name,
10
+ COUNT ( * ) AS taggings_count
11
+ FROM
12
+ <%= source_table_name %>
13
+ GROUP BY
14
+ name;
15
+ SQL
16
+ end
17
+
18
+ def down
19
+ execute <<-SQL
20
+ DROP VIEW <%= view_name %>;
21
+ SQL
22
+ end
23
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators"
4
+ require "rails/generators/active_record"
5
+
6
+ module Metka
7
+ module Generators
8
+ module Strategies
9
+ class ViewGenerator < ::Rails::Generators::Base # :nodoc:
10
+ include Rails::Generators::Migration
11
+
12
+ desc <<-LONGDESC
13
+ Generates migration to implement view strategy for Metka
14
+
15
+ > $ rails g metka:strategies:view --source-table-name=NAME_OF_TABLE_WITH_TAGS
16
+ LONGDESC
17
+
18
+ source_root File.expand_path("templates", __dir__)
19
+
20
+ class_option :source_table_name, type: :string, required: true,
21
+ desc: 'Name of the table that has a column with tags'
22
+
23
+ class_option :source_column_name, type: :string, default: 'tags',
24
+ desc: 'Name of the column with stored tags'
25
+
26
+ def generate_migration
27
+ migration_template "migration.rb.erb", "db/migrate/#{migration_name}.rb"
28
+ end
29
+
30
+ no_tasks do
31
+ def source_table_name
32
+ options[:source_table_name]
33
+ end
34
+
35
+ def source_column_name
36
+ options[:source_column_name]
37
+ end
38
+
39
+ def view_name
40
+ "tagged_#{source_table_name}"
41
+ end
42
+
43
+ def migration_name
44
+ "create_#{view_name}_view"
45
+ end
46
+
47
+ def migration_class_name
48
+ migration_name.classify
49
+ end
50
+ end
51
+
52
+ def self.next_migration_number(dir)
53
+ ::ActiveRecord::Generators::Base.next_migration_number(dir)
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "metka/version"
4
+
5
+ require "active_support/core_ext/module"
6
+ require "dry-configurable"
7
+
8
+ module Metka
9
+ require "metka/tag_list"
10
+ require "metka/generic_parser"
11
+ require "metka/query_builder"
12
+ require "metka/model"
13
+
14
+ class Error < StandardError; end
15
+
16
+ extend Dry::Configurable
17
+
18
+ setting :parser, Metka::GenericParser
19
+ setting :delimiter, ","
20
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "singleton"
4
+
5
+ module Metka
6
+ ##
7
+ # Returns a new Metka::TagList using the given tag string.
8
+ #
9
+ # Example:
10
+ # tag_list = Metka::GenericParser.instance.("One , Two, Three")
11
+ # tag_list # ["One", "Two", "Three"]
12
+ class GenericParser
13
+ include Singleton
14
+
15
+ def call(value)
16
+ TagList.new.tap do |tag_list|
17
+ case value
18
+ when String
19
+ tag_list.merge value.split(",").map(&:strip).reject(&:empty?)
20
+ when Enumerable
21
+ tag_list.merge value.reject(&:empty?)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+
5
+ module Metka
6
+ # Extends AR model with methods to use tags
7
+ module Model
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ scope :tagged_with, ->(tags, options = {}) do
12
+ tag_list = Metka.config.parser.instance.call(tags)
13
+ options = options.dup
14
+
15
+ return none if tag_list.empty?
16
+
17
+ where(::Metka::QueryBuilder.new.call(self, "tags", tag_list, options))
18
+ end
19
+ end
20
+
21
+ def tag_list=(v)
22
+ self.tags = Metka.config.parser.instance.call(v).to_a
23
+ self.tags = nil if tags.empty?
24
+ end
25
+
26
+ def tag_list
27
+ Metka.config.parser.instance.call(tags)
28
+ end
29
+
30
+ module ClassMethods # :nodoc:
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "query_builder/exclude_tags_query"
4
+ require_relative "query_builder/any_tags_query"
5
+ require_relative "query_builder/all_tags_query"
6
+
7
+ module Metka
8
+ class QueryBuilder
9
+ def call(taggable_model, column, tag_list, options)
10
+ if options[:exclude].present?
11
+ ExcludeTagsQuery.new(taggable_model, tag_model, tagging_model, tag_list, options).build
12
+ elsif options[:any].present?
13
+ AnyTagsQuery.instance.call(taggable_model, column, tag_list)
14
+ else
15
+ AllTagsQuery.instance.call(taggable_model, column, tag_list)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Metka
4
+ class AllTagsQuery
5
+ include Singleton
6
+
7
+ def call(model, column_name, tag_list)
8
+ column_cast = Arel::Nodes::NamedFunction.new(
9
+ "CAST",
10
+ [model.arel_table[column_name].as("text[]")]
11
+ )
12
+
13
+ value = Arel::Nodes::SqlLiteral.new(
14
+ # In Rails 5.2 and above Sanitanization moved to public level, but still we have to support 4.2 and 5.0 and 5.1
15
+ ActiveRecord::Base.send(:sanitize_sql_for_conditions, ["ARRAY[?]", tag_list.to_a])
16
+ )
17
+
18
+ value_cast = Arel::Nodes::NamedFunction.new(
19
+ "CAST",
20
+ [value.as("text[]")]
21
+ )
22
+
23
+ Arel::Nodes::InfixOperation.new("@>", column_cast, value_cast)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "singleton"
4
+
5
+ module Metka
6
+ class AnyTagsQuery
7
+ include Singleton
8
+
9
+ def call(model, column_name, tag_list)
10
+ column_cast = Arel::Nodes::NamedFunction.new(
11
+ "CAST",
12
+ [model.arel_table[column_name].as("text[]")]
13
+ )
14
+
15
+ value = Arel::Nodes::SqlLiteral.new(
16
+ # In Rails 5.2 and above Sanitanization moved to public level, but still we have to support 4.2 and 5.0 and 5.1
17
+ ActiveRecord::Base.send(:sanitize_sql_for_conditions, ["ARRAY[?]", tag_list.to_a])
18
+ )
19
+
20
+ value_cast = Arel::Nodes::NamedFunction.new(
21
+ "CAST",
22
+ [value.as("text[]")]
23
+ )
24
+
25
+ Arel::Nodes::InfixOperation.new("&&", column_cast, value_cast)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Metka
4
+ class ExcludeTagsQuery
5
+ end
6
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+
5
+ module Metka
6
+ class TagList < Set
7
+ # def add(o)
8
+ # if o.respond_to?(:each)
9
+ # o.each { |e| Metka.config.parser.call(e) }
10
+ # else
11
+ # super(Metka.config.parser.call(o))
12
+ # end
13
+ # end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Metka
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "metka/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "metka"
9
+ spec.version = Metka::VERSION
10
+ spec.authors = ["Igor Alexandrov"]
11
+ spec.email = ["igor.alexandrov@gmail.com"]
12
+
13
+ spec.summary = "Rails tagging system based on PostgreSQL arrays"
14
+ spec.description = "Rails tagging system based on PostgreSQL arrays"
15
+ spec.homepage = "https://github.com/jetrockets/metka"
16
+ spec.license = "MIT"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_dependency "dry-configurable"
28
+ spec.add_dependency "rails", ">= 4.2"
29
+
30
+ spec.add_development_dependency "ammeter"
31
+ spec.add_development_dependency "bundler"
32
+ spec.add_development_dependency "faker"
33
+ spec.add_development_dependency "pg"
34
+ spec.add_development_dependency "rake"
35
+ spec.add_development_dependency "rspec"
36
+ spec.add_development_dependency "rspec-rails"
37
+ spec.add_development_dependency "standard"
38
+ spec.add_development_dependency "timecop"
39
+ end
metadata ADDED
@@ -0,0 +1,221 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: metka
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Igor Alexandrov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-08-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-configurable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ammeter
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faker
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pg
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec-rails
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: standard
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: timecop
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description: Rails tagging system based on PostgreSQL arrays
168
+ email:
169
+ - igor.alexandrov@gmail.com
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - ".gitignore"
175
+ - ".rspec"
176
+ - ".rubocop.yml"
177
+ - ".travis.yml"
178
+ - CODE_OF_CONDUCT.md
179
+ - Gemfile
180
+ - Gemfile.lock
181
+ - LICENSE.txt
182
+ - README.md
183
+ - Rakefile
184
+ - bin/console
185
+ - bin/setup
186
+ - lib/generators/metka/strategies/view/templates/migration.rb.erb
187
+ - lib/generators/metka/strategies/view/view_generator.rb
188
+ - lib/metka.rb
189
+ - lib/metka/generic_parser.rb
190
+ - lib/metka/model.rb
191
+ - lib/metka/query_builder.rb
192
+ - lib/metka/query_builder/all_tags_query.rb
193
+ - lib/metka/query_builder/any_tags_query.rb
194
+ - lib/metka/query_builder/exclude_tags_query.rb
195
+ - lib/metka/tag_list.rb
196
+ - lib/metka/version.rb
197
+ - metka.gemspec
198
+ homepage: https://github.com/jetrockets/metka
199
+ licenses:
200
+ - MIT
201
+ metadata: {}
202
+ post_install_message:
203
+ rdoc_options: []
204
+ require_paths:
205
+ - lib
206
+ required_ruby_version: !ruby/object:Gem::Requirement
207
+ requirements:
208
+ - - ">="
209
+ - !ruby/object:Gem::Version
210
+ version: '0'
211
+ required_rubygems_version: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - ">="
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ requirements: []
217
+ rubygems_version: 3.0.3
218
+ signing_key:
219
+ specification_version: 4
220
+ summary: Rails tagging system based on PostgreSQL arrays
221
+ test_files: []