searchify 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -27,51 +27,77 @@ Usage
27
27
  -----
28
28
  ### Autocomplete search
29
29
 
30
- Searchify is intented to be used on a per model basis. You type, you choose, you are redirected to the chosen resource show page.
30
+ Searchify is intended to be used on a per model basis. You type, you choose, you are redirected to the chosen resource show page.
31
31
  Calling it on an index page, it infers the model to be searched with the controller name, or the `resource_class` helper, if it exists.
32
32
  For example, consider the following line of code on your `/posts` page:
33
33
 
34
- <%= autocomplete %>
34
+ <%= searchify %>
35
35
 
36
- This will triggers an AJAX call to /searchify/search/posts.json page and jQuery handles the response with its autocomplete widget.
36
+ This will triggers an AJAX call to `/searchify/search/posts.json` page and jQuery will handle the response with its autocomplete widget.
37
37
 
38
38
  If you want to specify the collection, i.e. searching for `users` on the `posts` page, just write this:
39
39
 
40
- <%= autocomplete :users %>
40
+ <%= searchify :users %>
41
41
 
42
- If you don't want to be redirected to the chosen resource page, you can specify the `select_url` option with the special `(id)` value.
43
- For example, to land on the edit page, you could write:
42
+ When a selection is made, you are redirected to the chosen resource show page. If you want to land on any other member action, the `action` option is for you.
44
43
 
45
- <%= autocomplete :select_url => "/posts/(id)/edit" %>
44
+ <%= searchify :action => :edit %>
45
+
46
+ If your redirect is more complex, you can always redefine the `select_url` option. The `(id)` keyword will be replaced by the id of the selected resource.
47
+
48
+ <%= searchify :select_url => "/more/complex/path/(id)/with/custom/action" %>
46
49
 
47
50
  ### In place autocomplete
48
51
 
49
- Searchify can also be used in a form. For example, let's say that a post belongs to a user of your choice.:
52
+ Searchify can also be used in a form. For example, let's say that a post belongs to a user of your choice:
50
53
 
51
54
  <%= form_for(@post) do |f| %>
52
55
  <div class="field">
53
56
  <%= f.label :user %><br />
54
- <%= f.autocomplete :user %>
57
+ <%= f.searchify :user %>
55
58
  </div>
56
59
  <% end %>
57
60
 
58
- Searchify will include a `user_id` field in your form, which will be automatically populated.
61
+ Searchify will include a `user_id` field in your form, which will be automatically populated with your search. Searchify uses the `label_method` option to display the object.
59
62
 
60
63
  ### Scopes
61
64
 
62
- Searchify is scopes aware. Let's say you are here:
65
+ Searchify is by default scopes aware. Let's say you are here:
63
66
 
64
67
  `/posts?created_by=3`
65
68
 
66
69
  Assuming your `Post` model responds to the `created_by` method, it will be included in the search.
67
70
 
71
+ You may also force your own scopes into a searchify field by adding the `scopes` options, as follow:
72
+
73
+ <%= searchify :scopes => {:created_by => 3} %>
74
+
68
75
  ### Configuration
69
76
 
70
77
  You can always override the defaults with an initializer. Options are as follow:
71
78
 
72
79
  Searchify::Config.configure do |config|
80
+
81
+ # Extract those keys from the params hash
73
82
  config.scope_exclusion = %w( controller action format collection term page )
74
- config.columns = %w( name title abbreviation )
83
+
84
+ # Default column names on which you want to do your search
85
+ config.column_names = %w( name title abbreviation )
86
+
87
+ # If true, searchify will apply url scopes to your search
88
+ config.scope_awareness = true
89
+
90
+ # Limit the number of results in one search
91
+ config.limit = 30
92
+
93
+ # Database search key. Default is 'ILIKE' for Postgres, 'LIKE' for others.
94
+ config.search_key = nil
95
+
96
+ # Method to be called on each resource
97
+ config.label_method = :name
98
+
99
+ # Default action on which you want to land after a selection. Could be :show, :edit or any custom member action
100
+ config.default_action = :show
75
101
  end
76
102
 
77
103
  ### Search stategies
@@ -13,9 +13,9 @@ module Searchify
13
13
  search_keyword = extract_search_key(resource_class)
14
14
 
15
15
  collection = if resource_class.respond_to?(:search_strategy)
16
- resource_class.search_strategy(search_term, current_scopes)
16
+ resource_class.search_strategy(search_term, searchify_scopes)
17
17
  else
18
- columns = Config.columns & resource_class.column_names
18
+ columns = Searchify::Config.column_names & resource_class.column_names
19
19
 
20
20
  scoped = resource_class.where( columns.map{ |c| "(#{c} #{search_keyword} :term)" }.join(' OR '), :term => "%#{search_term}%")
21
21
 
@@ -23,8 +23,8 @@ module Searchify
23
23
  scoped = scoped.send(key, value) if resource_class.respond_to?(key)
24
24
  end
25
25
 
26
- scoped.map do |resource|
27
- {:label => resource.name, :id => resource.id}
26
+ scoped.limit(Searchify::Config.limit).map do |resource|
27
+ {:label => resource.send(Searchify::Config.label_method), :id => resource.id}
28
28
  end
29
29
  end
30
30
 
@@ -34,11 +34,13 @@ module Searchify
34
34
  protected
35
35
 
36
36
  def extract_search_key(resource_class)
37
- case resource_class.connection.adapter_name
38
- when 'MySQL', 'Mysql2', 'SQLite'
39
- 'LIKE'
40
- when 'PostgreSQL'
41
- 'ILIKE'
37
+ Searchify::Config.search_key || begin
38
+ case resource_class.connection.adapter_name
39
+ when 'PostgreSQL'
40
+ 'ILIKE'
41
+ else
42
+ 'LIKE'
43
+ end
42
44
  end
43
45
  end
44
46
  end
@@ -1,13 +1,18 @@
1
1
  module Searchify
2
2
  module SearchifyHelper
3
- def autocomplete(*args)
3
+ def searchify(*args)
4
4
  options = args.extract_options!
5
5
 
6
- collection = options[:collection] || args.shift || extract_collection
7
- search_url = options[:search_url] || extract_search_url(collection, searchify_scopes)
8
- select_url = options[:select_url] || extract_select_url
9
-
10
- text_field_tag(:searchify, nil, :class => :searchify, :data => {:'select-url' => select_url, :'search-url' => search_url})
6
+ # searchify options
7
+ collection = options.delete(:collection) || args.shift || extract_collection
8
+ search_url = options.delete(:search_url) || extract_search_url(collection, options.delete(:scopes) || {})
9
+ select_url = options.delete(:select_url) || extract_select_url(options.delete(:action))
10
+
11
+ # tag options
12
+ options[:class] = [:searchify].push(options[:class]).flatten.compact
13
+ options[:data] = {:'select-url' => select_url, :'search-url' => search_url}.merge(options[:data])
14
+
15
+ text_field_tag(:searchify, nil, options)
11
16
  end
12
17
 
13
18
  protected
@@ -20,31 +25,51 @@ module Searchify
20
25
  end
21
26
  end
22
27
 
23
- def extract_search_url(collection, scopes)
24
- "/searchify/search/#{collection}.json?" + scopes.map{ |k,v| "#{k}=#{v}" }.join('&')
28
+ def extract_search_url(collection, scopes={})
29
+ url = "#{searchify_path}/search/#{collection}.json?"
30
+
31
+ scopes = searchify_scopes.merge(scopes) if Searchify::Config.scope_awareness
32
+
33
+ url << scopes.map{ |k,v| "#{k}=#{v}" }.join('&')
25
34
  end
26
35
 
27
- def extract_select_url
28
- if defined?(collection_path)
36
+ def extract_select_url(action)
37
+ url = if defined?(collection_path)
29
38
  "#{collection_path}/(id)"
30
39
  else
31
40
  "#{request.path}/(id)"
32
41
  end
42
+
43
+ action ||= Searchify::Config.default_action
44
+
45
+ unless [nil, :show, 'show'].include?(action)
46
+ url << "/#{action}"
47
+ end
48
+
49
+ url
33
50
  end
34
51
  end
35
52
  end
36
53
 
37
54
  class ActionView::Helpers::FormBuilder
38
55
 
39
- def autocomplete(field, *args)
56
+ def searchify(field, *args)
40
57
  options = args.extract_options!
41
58
 
42
- model_name = options[:model_name] || extract_model_name(field)
43
- field_name = options[:field_name] || extract_field_name(field)
44
- collection = options[:collection] || extract_collection(model_name)
45
- search_url = options[:search_url] || extract_search_url(collection)
59
+ # searchify options
60
+ model_name = options.delete(:model_name) || extract_model_name(field)
61
+ field_name = options.delete(:field_name) || extract_field_name(field)
62
+ collection = options.delete(:collection) || extract_collection(model_name)
63
+ search_url = options.delete(:search_url) || extract_search_url(collection, options.delete(:scopes) || {})
64
+
65
+ # field options
66
+ options[:class] = [:searchify].push(options[:class]).flatten.compact
67
+ options[:data] = {:'search-url' => search_url}.merge(options[:data])
68
+
69
+ # value
70
+ label_method = options.delete(:label_method) || Searchify::Config.label_method
46
71
 
47
- hidden_field(field_name) + @template.text_field_tag(:searchify, nil, :class => :searchify, :data => {:'search-url' => search_url})
72
+ hidden_field(field_name) + @template.text_field_tag(:searchify, object.send(model_name).try(label_method), options)
48
73
  end
49
74
 
50
75
  protected
@@ -53,8 +78,8 @@ class ActionView::Helpers::FormBuilder
53
78
  model_name.to_s.tableize
54
79
  end
55
80
 
56
- def extract_search_url(collection)
57
- "/searchify/search/#{collection}.json"
81
+ def extract_search_url(collection, scopes={})
82
+ "#{@template.searchify_path}/search/#{collection}.json?" + scopes.map{ |k,v| "#{k}=#{v}" }.join('&')
58
83
  end
59
84
 
60
85
  def extract_model_name(field)
data/config/routes.rb CHANGED
@@ -1,5 +1,3 @@
1
1
  Searchify::Engine.routes.draw do
2
- get "searchify/search"
3
-
4
2
  match '/search/:collection' => "searchify#search"
5
3
  end
@@ -1,7 +1,13 @@
1
1
  module Searchify
2
2
  module Config
3
3
  class << self
4
- attr_accessor :scope_exclusion, :columns
4
+ attr_accessor :scope_exclusion,
5
+ :column_names,
6
+ :scope_awareness,
7
+ :limit,
8
+ :search_key,
9
+ :label_method,
10
+ :default_action
5
11
 
6
12
  def configure(&block)
7
13
  @configuration = block
@@ -16,7 +22,12 @@ module Searchify
16
22
  def init!
17
23
  @defaults = {
18
24
  :@scope_exclusion => %w( controller action format collection term page ),
19
- :@columns => %w( name title abbreviation )
25
+ :@column_names => %w( name title abbreviation ),
26
+ :@scope_awareness => true,
27
+ :@limit => 30,
28
+ :@search_key => nil,
29
+ :@label_method => :name,
30
+ :@default_action => :show
20
31
  }
21
32
  end
22
33
 
@@ -10,7 +10,7 @@ module Searchify
10
10
 
11
11
  module InstanceMethods
12
12
  def searchify_scopes
13
- params.except(*Config.scope_exclusion)
13
+ params.except(*Searchify::Config.scope_exclusion)
14
14
  end
15
15
  end
16
16
  end
@@ -1,3 +1,3 @@
1
1
  module Searchify
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: searchify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
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: 2011-11-14 00:00:00.000000000Z
12
+ date: 2011-11-16 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &14547660 !ruby/object:Gem::Requirement
16
+ requirement: &2153533580 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.1.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *14547660
24
+ version_requirements: *2153533580
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: jquery-rails
27
- requirement: &14547200 !ruby/object:Gem::Requirement
27
+ requirement: &2153533140 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *14547200
35
+ version_requirements: *2153533140
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3
38
- requirement: &14546640 !ruby/object:Gem::Requirement
38
+ requirement: &2153532600 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *14546640
46
+ version_requirements: *2153532600
47
47
  description: Searchify provides a quick way to search your collections.
48
48
  email:
49
49
  - christ.blais@gmail.com
@@ -51,52 +51,51 @@ executables: []
51
51
  extensions: []
52
52
  extra_rdoc_files: []
53
53
  files:
54
- - app/helpers/searchify/searchify_helper.rb
55
- - app/controllers/searchify/searchify_controller.rb
56
54
  - app/assets/javascripts/searchify/searchify.js.coffee
55
+ - app/controllers/searchify/searchify_controller.rb
56
+ - app/helpers/searchify/searchify_helper.rb
57
57
  - config/routes.rb
58
- - lib/searchify/version.rb
59
- - lib/searchify/engine.rb
60
- - lib/searchify/controller.rb
61
58
  - lib/searchify/config.rb
59
+ - lib/searchify/controller.rb
60
+ - lib/searchify/engine.rb
61
+ - lib/searchify/version.rb
62
62
  - lib/searchify.rb
63
63
  - lib/tasks/searchify_tasks.rake
64
64
  - MIT-LICENSE
65
65
  - Rakefile
66
66
  - README.md
67
- - test/functional/searchify/searchify_controller_test.rb
68
- - test/unit/helpers/searchify/searchify_helper_test.rb
69
- - test/searchify_test.rb
70
- - test/dummy/config/database.yml
67
+ - test/dummy/app/assets/javascripts/application.js
68
+ - test/dummy/app/assets/stylesheets/application.css
69
+ - test/dummy/app/controllers/application_controller.rb
70
+ - test/dummy/app/helpers/application_helper.rb
71
+ - test/dummy/app/views/layouts/application.html.erb
71
72
  - test/dummy/config/application.rb
73
+ - test/dummy/config/boot.rb
74
+ - test/dummy/config/database.yml
72
75
  - test/dummy/config/environment.rb
73
- - test/dummy/config/initializers/session_store.rb
74
- - test/dummy/config/initializers/secret_token.rb
76
+ - test/dummy/config/environments/development.rb
77
+ - test/dummy/config/environments/production.rb
78
+ - test/dummy/config/environments/test.rb
79
+ - test/dummy/config/initializers/backtrace_silencers.rb
80
+ - test/dummy/config/initializers/inflections.rb
75
81
  - test/dummy/config/initializers/mime_types.rb
82
+ - test/dummy/config/initializers/secret_token.rb
83
+ - test/dummy/config/initializers/session_store.rb
76
84
  - test/dummy/config/initializers/wrap_parameters.rb
77
- - test/dummy/config/initializers/inflections.rb
78
- - test/dummy/config/initializers/backtrace_silencers.rb
79
- - test/dummy/config/routes.rb
80
85
  - test/dummy/config/locales/en.yml
81
- - test/dummy/config/environments/development.rb
82
- - test/dummy/config/environments/test.rb
83
- - test/dummy/config/environments/production.rb
84
- - test/dummy/config/boot.rb
85
- - test/dummy/Rakefile
86
+ - test/dummy/config/routes.rb
86
87
  - test/dummy/config.ru
87
- - test/dummy/app/helpers/application_helper.rb
88
- - test/dummy/app/controllers/application_controller.rb
89
- - test/dummy/app/views/layouts/application.html.erb
90
- - test/dummy/app/assets/stylesheets/application.css
91
- - test/dummy/app/assets/javascripts/application.js
92
- - test/dummy/log/development.log
93
88
  - test/dummy/public/404.html
94
- - test/dummy/public/favicon.ico
95
89
  - test/dummy/public/422.html
96
90
  - test/dummy/public/500.html
91
+ - test/dummy/public/favicon.ico
92
+ - test/dummy/Rakefile
97
93
  - test/dummy/script/rails
98
- - test/test_helper.rb
94
+ - test/functional/searchify/searchify_controller_test.rb
99
95
  - test/integration/navigation_test.rb
96
+ - test/searchify_test.rb
97
+ - test/test_helper.rb
98
+ - test/unit/helpers/searchify/searchify_helper_test.rb
100
99
  homepage: http://github.com/christianblais/searchify
101
100
  licenses: []
102
101
  post_install_message:
@@ -117,41 +116,40 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
116
  version: '0'
118
117
  requirements: []
119
118
  rubyforge_project:
120
- rubygems_version: 1.8.10
119
+ rubygems_version: 1.8.8
121
120
  signing_key:
122
121
  specification_version: 3
123
122
  summary: Search with ease!
124
123
  test_files:
125
- - test/functional/searchify/searchify_controller_test.rb
126
- - test/unit/helpers/searchify/searchify_helper_test.rb
127
- - test/searchify_test.rb
128
- - test/dummy/config/database.yml
124
+ - test/dummy/app/assets/javascripts/application.js
125
+ - test/dummy/app/assets/stylesheets/application.css
126
+ - test/dummy/app/controllers/application_controller.rb
127
+ - test/dummy/app/helpers/application_helper.rb
128
+ - test/dummy/app/views/layouts/application.html.erb
129
129
  - test/dummy/config/application.rb
130
+ - test/dummy/config/boot.rb
131
+ - test/dummy/config/database.yml
130
132
  - test/dummy/config/environment.rb
131
- - test/dummy/config/initializers/session_store.rb
132
- - test/dummy/config/initializers/secret_token.rb
133
+ - test/dummy/config/environments/development.rb
134
+ - test/dummy/config/environments/production.rb
135
+ - test/dummy/config/environments/test.rb
136
+ - test/dummy/config/initializers/backtrace_silencers.rb
137
+ - test/dummy/config/initializers/inflections.rb
133
138
  - test/dummy/config/initializers/mime_types.rb
139
+ - test/dummy/config/initializers/secret_token.rb
140
+ - test/dummy/config/initializers/session_store.rb
134
141
  - test/dummy/config/initializers/wrap_parameters.rb
135
- - test/dummy/config/initializers/inflections.rb
136
- - test/dummy/config/initializers/backtrace_silencers.rb
137
- - test/dummy/config/routes.rb
138
142
  - test/dummy/config/locales/en.yml
139
- - test/dummy/config/environments/development.rb
140
- - test/dummy/config/environments/test.rb
141
- - test/dummy/config/environments/production.rb
142
- - test/dummy/config/boot.rb
143
- - test/dummy/Rakefile
143
+ - test/dummy/config/routes.rb
144
144
  - test/dummy/config.ru
145
- - test/dummy/app/helpers/application_helper.rb
146
- - test/dummy/app/controllers/application_controller.rb
147
- - test/dummy/app/views/layouts/application.html.erb
148
- - test/dummy/app/assets/stylesheets/application.css
149
- - test/dummy/app/assets/javascripts/application.js
150
- - test/dummy/log/development.log
151
145
  - test/dummy/public/404.html
152
- - test/dummy/public/favicon.ico
153
146
  - test/dummy/public/422.html
154
147
  - test/dummy/public/500.html
148
+ - test/dummy/public/favicon.ico
149
+ - test/dummy/Rakefile
155
150
  - test/dummy/script/rails
156
- - test/test_helper.rb
151
+ - test/functional/searchify/searchify_controller_test.rb
157
152
  - test/integration/navigation_test.rb
153
+ - test/searchify_test.rb
154
+ - test/test_helper.rb
155
+ - test/unit/helpers/searchify/searchify_helper_test.rb
File without changes