indago 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: 25248eebba2bf6fd040038720bd0501778c6b8ed32a8cca0b534bc7e538b95a5
4
+ data.tar.gz: df1356fb937196e84c4276ab000b41924479d97a8c33064eca7edf54604c1cbc
5
+ SHA512:
6
+ metadata.gz: '068cf27997b839b66c2cec3439201d6c808ab8550a3b84df0cdfeee27d1cbb0156d6f3afd9f76a34ba2924058c932d9ec0e0fce23427f56d2ac8ac9a1554985b'
7
+ data.tar.gz: 60b60f2249b2273e880dbdb3f049b40a22c0958050bba90e05d374ba5639227d45becef6722961a3dc60513407fabecd8c4911da4e9de8912fe836cc6273b2a7
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in indago.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Nebulab SRLs
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,67 @@
1
+ # Indago
2
+
3
+ Lightweight drop-in Ransack replacement without the ActiveRecord hackery.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'indago'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ ## Usage
18
+
19
+ In your controller:
20
+
21
+ ```rb
22
+ require "indago"
23
+
24
+ class PostsController < ApplicationController
25
+ SEARCH_FIELDS = [
26
+ # Reusing scopes
27
+ Indago::Field.new(:is_published) do |scope, value|
28
+ value.present? ? scope.published : scope
29
+ end,
30
+
31
+ # Complex joins
32
+ Indago::Field.new(:commented_by_id) do |scope, value|
33
+ scope.where(
34
+ id: Comment.where(author_id: value).select(:post_id).distinct
35
+ )
36
+ end,
37
+
38
+ # Common search fields
39
+ Indago::Field::Contain.new(:title_cont, :title),
40
+ Indago::Field::Contain.new(:body_cont, :body),
41
+
42
+ # Custom queries
43
+ Indago::Field.new(:body_starts_with) do |scope, value|
44
+ scope.where("body LIKE ?", "#{value}%")
45
+ end
46
+ ]
47
+
48
+ def index
49
+ @search = Indago::Search.new(Post.all, SEARCH_FIELDS, params[:q])
50
+ @posts = @search.result
51
+ end
52
+ end
53
+ ```
54
+
55
+ ## Development
56
+
57
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
58
+
59
+ 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).
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nebulab/indago.
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "indago"
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ require_relative 'lib/indago/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "indago"
5
+ spec.version = Indago::VERSION
6
+ spec.authors = ["Elia Schito", "Filippo Liverani"]
7
+ spec.email = ["eliaschito@nebulab.it", "filippoliverani@nebulab.it"]
8
+
9
+ spec.summary = %q{Lightweight drop-in Ransack replacement without the ActiveRecord hackery.}
10
+ spec.homepage = "https://github.com/nebulab/indago#readme"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = "https://github.com/nebulab/indago"
16
+ spec.metadata["changelog_uri"] = "https://github.com/nebulab/indago/releases"
17
+ spec.metadata["copyright"] = "Nebulab SRLs"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ files = Dir.chdir(File.expand_path('..', __FILE__)) { `git ls-files -z`.split("\x0") }
22
+
23
+ spec.files = files.reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ spec.bindir = "exe"
25
+ spec.executables = files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+ end
@@ -0,0 +1,9 @@
1
+ require "indago/version"
2
+
3
+ module Indago
4
+ class Error < StandardError; end
5
+
6
+ autoload :Field, "indago/field"
7
+ autoload :Hackery, "indago/hackery"
8
+ autoload :Search, "indago/search"
9
+ end
@@ -0,0 +1,21 @@
1
+ class Indago::Field
2
+ autoload :Contain, 'indago/field/contain'
3
+
4
+ attr_reader :name
5
+
6
+ def initialize(name, &block)
7
+ @name = name
8
+ @block = block
9
+ end
10
+
11
+ def call(scope, value)
12
+ @block.call(scope, value)
13
+ end
14
+
15
+ def self.for(name)
16
+ case name.to_s
17
+ when /^(\w+)_cont$/ then Contain.new(name.to_sym, $1.to_sym)
18
+ else raise Indago::Error, "Cannot find a proper field for #{name.inspect}"
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ class Indago::Field::Contain
2
+ attr_reader :name
3
+
4
+ # @example
5
+ # Indago::Fields::Contain.new(:title)
6
+ #
7
+ def initialize(name, attribute)
8
+ @name = name || :"#{attribute}_cont"
9
+ @attribute = attribute
10
+ end
11
+
12
+ def call(scope, value)
13
+ scope.where("#{@attribute} LIKE ?", "%#{value}%")
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ # BEWARE of the Hackery!
2
+ module Indago::Hackery
3
+ extend self
4
+
5
+ def install!
6
+ Indago::Search.prepend Search
7
+ ActiveRecord::Base.singleton_class.prepend Base
8
+ end
9
+
10
+ module Search
11
+ # Pretend to be a Ransack::Search
12
+ def is_a?(klass)
13
+ return true if defined?(Ransack::Search) && klass == Ransack::Search
14
+ super
15
+ end
16
+ end
17
+
18
+ module Base
19
+ def ransack(params)
20
+ fields = params.keys.map { |name| Indago::Fields.for(name) }
21
+ Indago::Search.new(all, fields, params)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,29 @@
1
+ class Indago::Search
2
+ def initialize(scope, fields, params)
3
+ @scope = scope
4
+ @params = params.presence || {}
5
+ @fields = fields
6
+ @field_names = @fields.map { |field| field.name.to_sym }
7
+ end
8
+
9
+ def result
10
+ @result ||= @fields.reduce(@scope) do |scope, field|
11
+ value = @params[field.name]
12
+ value.present? ? field.call(scope, value) : scope
13
+ end
14
+ end
15
+
16
+ def method_missing(name, *args, &block)
17
+ if @field_names.include?(name) && args.empty?
18
+ @params[name]
19
+ elsif @scope.respond_to?(name)
20
+ @scope.send(name, *args, &block)
21
+ else
22
+ super
23
+ end
24
+ end
25
+
26
+ def respond_to_missing?(name, include_private = false)
27
+ @field_names.include?(name) || @scope.respond_to?(name) || super
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module Indago
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: indago
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Elia Schito
8
+ - Filippo Liverani
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2020-11-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email:
16
+ - eliaschito@nebulab.it
17
+ - filippoliverani@nebulab.it
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - ".gitignore"
23
+ - ".travis.yml"
24
+ - Gemfile
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - bin/setup
30
+ - indago.gemspec
31
+ - lib/indago.rb
32
+ - lib/indago/field.rb
33
+ - lib/indago/field/contain.rb
34
+ - lib/indago/hackery.rb
35
+ - lib/indago/search.rb
36
+ - lib/indago/version.rb
37
+ homepage: https://github.com/nebulab/indago#readme
38
+ licenses:
39
+ - MIT
40
+ metadata:
41
+ homepage_uri: https://github.com/nebulab/indago#readme
42
+ source_code_uri: https://github.com/nebulab/indago
43
+ changelog_uri: https://github.com/nebulab/indago/releases
44
+ copyright: Nebulab SRLs
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.5.0
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.1.4
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Lightweight drop-in Ransack replacement without the ActiveRecord hackery.
64
+ test_files: []