facet_for 0.0.8 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +58 -0
- data/lib/facet_for/version.rb +1 -1
- data/lib/facet_for.rb +11 -6
- metadata +7 -7
- data/README.textile +0 -8
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# facet_for
|
2
|
+
|
3
|
+
facet_for is a collection of FormBuilder helpers that speed up the process of creating complex search forms with [Ransack](http://github.com/ernie/ransack).
|
4
|
+
|
5
|
+
For many searches, it can be as simple as:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
<%= f.facet_for(:field_name) -%>
|
9
|
+
```
|
10
|
+
|
11
|
+
facet_for will use the database column type to choose an appropriate Ransack predicate, then create both the label and field. It will also work for ```has_many``` and ```belongs_to``` associations.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add ```gem 'facet_for'``` in your Gemfile
|
16
|
+
|
17
|
+
## Options
|
18
|
+
|
19
|
+
Many options in facet_for can be overridden by passing hash options.
|
20
|
+
|
21
|
+
### Predicates
|
22
|
+
|
23
|
+
To choose an alternate predicate, pass ```:type => :predicate```.
|
24
|
+
|
25
|
+
Most of the Ransack predicates are supported:
|
26
|
+
|
27
|
+
* Contains - ```:type => :cont```
|
28
|
+
* Doesn't Contain - ```:type => :not_cont```
|
29
|
+
* Starts With - ```:type => :start```
|
30
|
+
* Doesn't Start With - ```:type => :not_start```
|
31
|
+
* Ends With - ```:type => :end```
|
32
|
+
* Doesn't End With - ```:type => :not_end```
|
33
|
+
* Is Null - ```:type => :null```
|
34
|
+
* Is Not Null - ```:type => :not_null```
|
35
|
+
* True - ```:type => :true```
|
36
|
+
* False - ```:type => :false```
|
37
|
+
* Greater Than or Equal - ```:type => :gte```
|
38
|
+
* Less Than or Equal - ```:type => :lte```
|
39
|
+
|
40
|
+
As well as several special cases and meta-predicates specific to facet_for:
|
41
|
+
|
42
|
+
#### Collections
|
43
|
+
|
44
|
+
* Collection - ```:type => :collection```
|
45
|
+
|
46
|
+
By default, when passing a ```has_many``` or ```belongs_to``` association into facet_for, it will choose the ```:collection``` predicate. This produces a ```collection_select``` with the unique options for that association.
|
47
|
+
|
48
|
+
When using ```:type => :collection``` on a column in the database, rather than an association, it defaults to all unique options.
|
49
|
+
|
50
|
+
To pass specific options, use ```:collection => []``` to pass an array of options. This may either be a flat array of strings (eg ```['Blue', 'Red', 'Green']), a flat array of objects (```SomeModel.all```), or a nested array of name and value pairs.
|
51
|
+
|
52
|
+
* Contains Any - ```:type => :cont_any```
|
53
|
+
|
54
|
+
```:type => :cont_any``` behaves in a nearly identical manner to ```:type => :collection```, except that the collection is rendered as a series of check boxes rather than a ```collection_select```.
|
55
|
+
|
56
|
+
### Custom Labels
|
57
|
+
|
58
|
+
By default, facet_for will determine label text based on a combination of the field name and the predicate. This can be overwritten by specifying ```:label => 'Your Label Here'```.
|
data/lib/facet_for/version.rb
CHANGED
data/lib/facet_for.rb
CHANGED
@@ -37,7 +37,8 @@ module FacetFor
|
|
37
37
|
|
38
38
|
q.context.searchable_associations.each do |association|
|
39
39
|
association_model = association.camelcase.singularize.constantize
|
40
|
-
column_options.push([association_model.to_s,
|
40
|
+
column_options.push([association_model.to_s,
|
41
|
+
options_for_model(association_model, association)])
|
41
42
|
end
|
42
43
|
|
43
44
|
column_options
|
@@ -96,13 +97,13 @@ module FacetFor
|
|
96
97
|
x.primary_key_name == @facet[:column_name].to_s }.uniq and association.count > 0
|
97
98
|
|
98
99
|
if association.first.macro == :has_many
|
99
|
-
|
100
100
|
# For a has_many relationship, we want the plural name with _id.
|
101
101
|
# Ransack will then look at the _id column for the associated model.
|
102
102
|
# This won't work properly on models with nonstandard id column
|
103
103
|
# names. That's a problem, but whatevs for the time being.
|
104
104
|
@facet[:association_name] = "#{association.first.plural_name}"
|
105
105
|
@facet[:column_name] = "#{association.first.plural_name}_id"
|
106
|
+
@facet[:clean_column_name] = "#{association.first.name.to_s.singularize}_id"
|
106
107
|
|
107
108
|
elsif association.first.macro == :belongs_to
|
108
109
|
|
@@ -121,11 +122,11 @@ module FacetFor
|
|
121
122
|
@facet[:type] = @facet[:type] || :collection
|
122
123
|
|
123
124
|
# If the user hasn't specified a collection, we'll provide one now
|
124
|
-
# based on this association
|
125
|
+
# based on this association. We only want to use distinct values.
|
126
|
+
# This could probably be cleaner, but it works.
|
125
127
|
|
126
|
-
@facet[:collection] = @facet[:model].joins(@facet[:association_name].to_sym).select("DISTINCT #{
|
128
|
+
@facet[:collection] = @facet[:model].unscoped.joins(@facet[:association_name].to_sym).select("DISTINCT #{clean_column}").where("#{clean_column} IS NOT NULL").map { |m| @facet[:association_name].to_s.singularize.camelcase.constantize.find(m.send(clean_column)) }
|
127
129
|
|
128
|
-
# @facet[:collection] = @facet[:collection] || association.first.klass.all
|
129
130
|
end
|
130
131
|
else
|
131
132
|
|
@@ -349,8 +350,12 @@ module FacetFor
|
|
349
350
|
|
350
351
|
end
|
351
352
|
|
353
|
+
def clean_column
|
354
|
+
@facet[:clean_column_name] || @facet[:column_name]
|
355
|
+
end
|
356
|
+
|
352
357
|
def unique_value_collection
|
353
|
-
@facet[:collection] = @facet[:model].select("DISTINCT #{
|
358
|
+
@facet[:collection] = @facet[:model].unscoped.select("DISTINCT #{clean_column}").where("#{clean_column} IS NOT NULL").map { |m| m.send(@facet[:column_name]) }
|
354
359
|
end
|
355
360
|
|
356
361
|
def label(string_name, string_label = nil)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: facet_for
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-01-
|
12
|
+
date: 2012-01-09 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: ransack
|
16
|
-
requirement: &
|
16
|
+
requirement: &84788010 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *84788010
|
25
25
|
description: Provides helpers for creating search forms with Ransack
|
26
26
|
email:
|
27
27
|
- jbarket@sleepunit.com
|
@@ -31,7 +31,7 @@ extra_rdoc_files: []
|
|
31
31
|
files:
|
32
32
|
- .gitignore
|
33
33
|
- Gemfile
|
34
|
-
- README.
|
34
|
+
- README.md
|
35
35
|
- Rakefile
|
36
36
|
- facet_for.gemspec
|
37
37
|
- lib/facet_for.rb
|
@@ -52,7 +52,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
52
52
|
version: '0'
|
53
53
|
segments:
|
54
54
|
- 0
|
55
|
-
hash:
|
55
|
+
hash: 432141005
|
56
56
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
57
|
none: false
|
58
58
|
requirements:
|
@@ -61,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
61
|
version: '0'
|
62
62
|
segments:
|
63
63
|
- 0
|
64
|
-
hash:
|
64
|
+
hash: 432141005
|
65
65
|
requirements: []
|
66
66
|
rubyforge_project: facet_for
|
67
67
|
rubygems_version: 1.8.10
|
data/README.textile
DELETED