ransack_wrap 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6908a42e14d663ae9e5a54fbc96455d9943095ba
4
+ data.tar.gz: 57b0916cabb45e9355c7030e183c9aa032e38960
5
+ SHA512:
6
+ metadata.gz: 20d1537599c512c796603f8a436dc7c3a4a099af2214dc565c84e059d39a950bbf6e9174f7f452ce68ff5a9b71da1c5ec89c587a4d8b6e7331c29955752f6779
7
+ data.tar.gz: 1696c0151a1345f5d85cff230e3a99204496246ef95a93001f6742a4c6a1a13833b43f3270a1fd0579c4b26e50866d094d9878d6e84fb785ea626526d6d6e89c
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ransack_wrap.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Tõnis Simo
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # RansackWrap
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ransack_wrap'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ransack_wrap
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ module Rails
2
+ module Generators
3
+ class SearcherGenerator < NamedBase
4
+ source_root File.expand_path("../templates", __FILE__)
5
+ check_class_collision suffix: "Searcher"
6
+
7
+ def create_searcher_file
8
+ template 'searcher.rb', File.join('app/searchers', class_path, "#{file_name}_searcher.rb")
9
+ end
10
+
11
+ # hook_for :test_framework
12
+ unless methods.include?(:module_namespacing)
13
+ def module_namespacing
14
+ yield if block_given?
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,28 @@
1
+ <%- module_namespacing do -%>
2
+ class <%= class_name %>Searcher < RansackWrap::Search
3
+ class Scopes < RansackWrap::Scopes
4
+ # Custom searchable keys
5
+ # attribute :only_sale, type: ActiveAttr::Typecasting::Boolean
6
+ # attribute :opt_in
7
+ # attribute :cat_in
8
+
9
+ # Custom searchable method for key :only_sale with scope
10
+ # def scope_only_sale(value)
11
+ # @object.joins_discount
12
+ # end
13
+
14
+ # Custom searchable method for key :opt_in with values for complex scope
15
+ # def scope_opt_in(values)
16
+ # @object.with_options_intersect(values.values)
17
+ # end
18
+
19
+ # Custom searchable method for key :cat_in with simple conditions
20
+ # def scope_cat_in(value)
21
+ # @object.where(category_id: value)
22
+ # end
23
+ end
24
+
25
+ # Delegation to searchers
26
+ # delegate :only_sale, :opt_in, :cat_in, to: :base
27
+ end
28
+ <% end -%>
@@ -0,0 +1,12 @@
1
+ require "ransack_wrap/version"
2
+ require "ransack_wrap/search"
3
+ require "ransack_wrap/scopes"
4
+ require 'ransack_wrap/adapters/active_record'
5
+ require 'ransack_wrap/helpers'
6
+ require 'ransack_wrap/railtie' if defined?(Rails)
7
+
8
+ module RansackWrap
9
+ # Your code goes here...
10
+ end
11
+
12
+ ActionController::Base.helper RansackWrap::Helpers::FormHelper
@@ -0,0 +1,26 @@
1
+ require 'active_record'
2
+
3
+ module RansackWrap
4
+ module Adapters
5
+ module ActiveRecord
6
+ module Base
7
+ def wrap_searcher_as(name, params = {})
8
+ send(:searcher_class_for, name).new(self, params)
9
+ end
10
+
11
+ def wrap_searcher(params = {})
12
+ send :wrap_searcher_as, nil, params
13
+ end
14
+
15
+ private
16
+ def searcher_class_for(name)
17
+ name = name.to_s.camelize unless name.nil?
18
+ name ||= self.scoped.klass.name
19
+ name.concat("Searcher").constantize
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ ActiveRecord::Base.extend RansackWrap::Adapters::ActiveRecord::Base
@@ -0,0 +1,2 @@
1
+ require 'ransack/helpers/form_builder'
2
+ require 'ransack_wrap/helpers/form_helper'
@@ -0,0 +1,30 @@
1
+ module RansackWrap
2
+ module Helpers
3
+ module FormHelper
4
+ def search_form_for(record, options = {}, &proc)
5
+ if record.is_a?(Ransack::Search) || record.is_a?(RansackWrap::Search)
6
+ search = record
7
+ options[:url] ||= polymorphic_path(search.klass)
8
+ elsif record.is_a?(Array) && (search = record.detect {|o| o.is_a?(Ransack::Search) || o.is_a?(RansackWrap::Search)})
9
+ options[:url] ||= polymorphic_path(record.map {|o|
10
+ o.is_a?(Ransack::Search) || record.is_a?(RansackWrap::Search) ? o.klass : o
11
+ })
12
+ else
13
+ raise ArgumentError, "No Ransack::Search or RansackWrap::Search object was provided to search_form_for!"
14
+ end
15
+ options[:html] ||= {}
16
+ html_options = {
17
+ :class => options[:class].present? ? "#{options[:class]}" : "#{search.klass.to_s.underscore}_search",
18
+ :id => options[:id].present? ? "#{options[:id]}" : "#{search.klass.to_s.underscore}_search",
19
+ :method => :get
20
+ }
21
+ options[:as] ||= 'q'
22
+ options[:html].reverse_merge!(html_options)
23
+ options[:builder] ||= Ransack::Helpers::FormBuilder
24
+
25
+ form_for(record, options, &proc)
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,19 @@
1
+ require 'rails/railtie'
2
+
3
+ module ActiveModel
4
+ class Railtie < Rails::Railtie
5
+ generators do |app|
6
+ app ||= Rails.application # Rails 3.0.x does not yield `app`
7
+
8
+ Rails::Generators.configure! app.config.generators
9
+ end
10
+ end
11
+ end
12
+
13
+ module RansackWrap
14
+ class Railtie < Rails::Railtie
15
+ config.after_initialize do |app|
16
+ app.config.paths.add 'app/searchers', eager_load: true
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ module RansackWrap
2
+ class Scopes
3
+ include ActiveAttr::TypecastedAttributes
4
+ attr_reader :object
5
+
6
+ def initialize(object)
7
+ @object = object
8
+ end
9
+
10
+ def scoped
11
+ object.scoped
12
+ end
13
+
14
+ def attribute_method?(attr_name)
15
+ respond_to_without_attributes?(:attributes) && attr_name.in?(attributes)
16
+ end
17
+
18
+ def try_scope(name, args)
19
+ return unless args.present?
20
+ self.try "scope_#{name}", args
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,63 @@
1
+ module RansackWrap
2
+ class Search
3
+ include Ransack::Naming
4
+
5
+ attr_reader :base
6
+
7
+ def initialize(object, params = {})
8
+ @base = scope_class.new(object)
9
+
10
+ params ||= {}
11
+ ransack_params = params.slice! *base.attributes.keys.map(&:to_sym)
12
+ ransack_params ||= {}
13
+
14
+ build params.with_indifferent_access
15
+ ransack.build ransack_params.with_indifferent_access
16
+ end
17
+
18
+ def ransack
19
+ @ransack ||= build_for_ransack.ransack
20
+ end
21
+
22
+ def result
23
+ ransack.result
24
+ end
25
+
26
+ def build(params = {})
27
+ params.each {|key, value| base.send "#{key}=", value }
28
+ end
29
+
30
+ def build_for_ransack
31
+ base.attributes
32
+ .collect {|name, args| base.try_scope name, args }.compact
33
+ .inject(base.scoped) {|scope, subq| scope.merge subq }
34
+ end
35
+
36
+ def method_missing(method, *args)
37
+ name = method.to_s
38
+ writer = name.sub! /\=$/, ''
39
+
40
+ if base.attribute_method? name
41
+ base.send method, *args
42
+ elsif ransack.respond_to? name
43
+ ransack.send method, *args
44
+ else
45
+ super
46
+ end
47
+ end
48
+
49
+ def respond_to?(method, include_private = false)
50
+ super or begin
51
+ name = method.to_s
52
+ writer = name.sub!(/\=$/, '')
53
+ base.attribute_method?(name) || ransack.respond_to?(name) || false
54
+ end
55
+ end
56
+
57
+ private
58
+ def scope_class
59
+ raise NameError unless self.class.const_defined?(:Scopes)
60
+ self.class.const_get(:Scopes)
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,3 @@
1
+ module RansackWrap
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'ransack_wrap/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "ransack_wrap"
9
+ spec.version = RansackWrap::VERSION
10
+ spec.authors = ["Tõnis Simo"]
11
+ spec.email = ["anton.estum@gmail.com"]
12
+ spec.description = %q{Model-wrappers to customize ransack search}
13
+ spec.summary = %q{Ransack Wrap helps to customize Ransack searching for each models, it allows to easy overwrite and add search keys, scopes and queries}
14
+ spec.homepage = "http://github.com/estum/ransack_wrap"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'ransack', '>= 1.0.0'
23
+ spec.add_dependency 'active_attr', '>= 0.8.2'
24
+ spec.add_dependency 'activerecord', '>= 3.0.2', "< 4.0"
25
+ spec.add_dependency 'actionpack', '>= 3.0.2', "< 4.0"
26
+ spec.add_runtime_dependency "activemodel", ">= 3.0.2", "< 4.0"
27
+ spec.add_runtime_dependency "activesupport", ">= 3.0.2", "< 4.0"
28
+ spec.add_dependency 'polyamorous', '~> 0.6.0'
29
+ spec.add_development_dependency "bundler", "~> 1.3"
30
+ spec.add_development_dependency "rake"
31
+ end
metadata ADDED
@@ -0,0 +1,212 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ransack_wrap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tõnis Simo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ransack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: active_attr
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.8.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.8.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.2
48
+ - - <
49
+ - !ruby/object:Gem::Version
50
+ version: '4.0'
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - '>='
56
+ - !ruby/object:Gem::Version
57
+ version: 3.0.2
58
+ - - <
59
+ - !ruby/object:Gem::Version
60
+ version: '4.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: actionpack
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - '>='
66
+ - !ruby/object:Gem::Version
67
+ version: 3.0.2
68
+ - - <
69
+ - !ruby/object:Gem::Version
70
+ version: '4.0'
71
+ type: :runtime
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 3.0.2
78
+ - - <
79
+ - !ruby/object:Gem::Version
80
+ version: '4.0'
81
+ - !ruby/object:Gem::Dependency
82
+ name: activemodel
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: 3.0.2
88
+ - - <
89
+ - !ruby/object:Gem::Version
90
+ version: '4.0'
91
+ type: :runtime
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: 3.0.2
98
+ - - <
99
+ - !ruby/object:Gem::Version
100
+ version: '4.0'
101
+ - !ruby/object:Gem::Dependency
102
+ name: activesupport
103
+ requirement: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - '>='
106
+ - !ruby/object:Gem::Version
107
+ version: 3.0.2
108
+ - - <
109
+ - !ruby/object:Gem::Version
110
+ version: '4.0'
111
+ type: :runtime
112
+ prerelease: false
113
+ version_requirements: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: 3.0.2
118
+ - - <
119
+ - !ruby/object:Gem::Version
120
+ version: '4.0'
121
+ - !ruby/object:Gem::Dependency
122
+ name: polyamorous
123
+ requirement: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ~>
126
+ - !ruby/object:Gem::Version
127
+ version: 0.6.0
128
+ type: :runtime
129
+ prerelease: false
130
+ version_requirements: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ~>
133
+ - !ruby/object:Gem::Version
134
+ version: 0.6.0
135
+ - !ruby/object:Gem::Dependency
136
+ name: bundler
137
+ requirement: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ version: '1.3'
142
+ type: :development
143
+ prerelease: false
144
+ version_requirements: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ~>
147
+ - !ruby/object:Gem::Version
148
+ version: '1.3'
149
+ - !ruby/object:Gem::Dependency
150
+ name: rake
151
+ requirement: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - '>='
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ type: :development
157
+ prerelease: false
158
+ version_requirements: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - '>='
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ description: Model-wrappers to customize ransack search
164
+ email:
165
+ - anton.estum@gmail.com
166
+ executables: []
167
+ extensions: []
168
+ extra_rdoc_files: []
169
+ files:
170
+ - .gitignore
171
+ - Gemfile
172
+ - LICENSE.txt
173
+ - README.md
174
+ - Rakefile
175
+ - lib/generators/rails/searcher_generator.rb
176
+ - lib/generators/rails/templates/searcher.rb
177
+ - lib/ransack_wrap.rb
178
+ - lib/ransack_wrap/adapters/active_record.rb
179
+ - lib/ransack_wrap/helpers.rb
180
+ - lib/ransack_wrap/helpers/form_helper.rb
181
+ - lib/ransack_wrap/railtie.rb
182
+ - lib/ransack_wrap/scopes.rb
183
+ - lib/ransack_wrap/search.rb
184
+ - lib/ransack_wrap/version.rb
185
+ - ransack_wrap.gemspec
186
+ homepage: http://github.com/estum/ransack_wrap
187
+ licenses:
188
+ - MIT
189
+ metadata: {}
190
+ post_install_message:
191
+ rdoc_options: []
192
+ require_paths:
193
+ - lib
194
+ required_ruby_version: !ruby/object:Gem::Requirement
195
+ requirements:
196
+ - - '>='
197
+ - !ruby/object:Gem::Version
198
+ version: '0'
199
+ required_rubygems_version: !ruby/object:Gem::Requirement
200
+ requirements:
201
+ - - '>='
202
+ - !ruby/object:Gem::Version
203
+ version: '0'
204
+ requirements: []
205
+ rubyforge_project:
206
+ rubygems_version: 2.0.5
207
+ signing_key:
208
+ specification_version: 4
209
+ summary: Ransack Wrap helps to customize Ransack searching for each models, it allows
210
+ to easy overwrite and add search keys, scopes and queries
211
+ test_files: []
212
+ has_rdoc: