hobo_bootstrap_ui 2.0.0.pre6 → 2.0.0.pre7

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.0.pre6
1
+ 2.0.0.pre7
@@ -0,0 +1,71 @@
1
+ <!-- An `<input type="text">` with auto-completion. Allows the user to chose the target of a `belongs_to` association by name.
2
+
3
+ This tag relies on an autocompleter being defined in a controller. A simple example:
4
+
5
+ <form with="&ProjectMembership.new">
6
+ <name-one:user>
7
+ </form>
8
+
9
+ class ProjectMembership < ActiveRecord::Base
10
+ hobo_model
11
+ belongs_to :user
12
+ end
13
+
14
+ class User < ActiveRecord::Base
15
+ hobo_user_model
16
+ has_many :project_memberships, :accessible => true, :dependent => :destroy
17
+ end
18
+
19
+ class UsersController < ApplicationController
20
+ autocomplete
21
+ end
22
+
23
+ The route used by the autocompleter looks something like `/users/complete_name`. The first part of this route is specified by the `complete-target` attribute, and the second part is specified by the `completer` attribute.
24
+
25
+ `complete-target` specifies the controller for the route. It can be specified by either supplying a model class or a model. If a model is supplied, the id of the model is passed as a parameter to the controller. (`?id=7`, for example) The default for this attribute is the class of the context. In other words, the class that contains the `has_many / has_one`, not the class with the `belongs_to`.
26
+
27
+ `completer` specifies the action for the route. `name-one` prepends `complete_` to the value given here. This should be exactly the same as the first parameter to `autocomplete` in your controller. As an example: `autocomplete :email_address` would correspond to `completer="email_address"`. The default for this attribute is the name field for the model being searched, which is usually `name`, but not always.
28
+
29
+ The query string is passed to the controller in the `term` parameter. (`?term=hello` for example).
30
+
31
+ For more information on how to customize the controller, see the [controller manual](http://cookbook.hobocentral.net/manual/controllers#autocompleters)
32
+
33
+ Here's a more complex example. This used to be a part of [agility](http://cookbook.hobocentral.net/tutorials/agility) until it was simplified.
34
+
35
+ class ProjectsController < ApplicationController
36
+ autocomplete :new_member_name do
37
+ project = find_instance
38
+ hobo_completions :name, User.without_project(project).is_not(project.owner)
39
+ end
40
+ end
41
+
42
+ Note that this was added to the projects controller, rather than the users controller as in the first example. You can read this as: create an auto-complete action called `new_member_name` that finds users that are not already members of the project, and not the owner of the project, and completes the :name field.
43
+
44
+ <name-one:user complete-target="&@project" completer="new_member_name"/>
45
+
46
+ We're using an object as the complete-target rather than a class. This allows the `find_instance` in our controller action to function.
47
+
48
+ There's another example of `<name-one>` use in the [recipes](http://cookbook.hobocentral.net/recipes/36-using-a-name-one-one-a).
49
+
50
+ ### Attributes:
51
+
52
+ All other attributes are passed through to the `<typeahead>` that provides the core functionality for this widget.
53
+
54
+ - `complete-target`, `completer`: see above
55
+ - `min-chars`: The minimum number of characters that must be entered in the input field before an Ajax request is made. A compatibility alias for `data-minLength`
56
+ - `nil-value`: If there is no current value, this text will appear greyed out inside the control, and will disappear when text is entered. This is a compatibility alias for `placeholder`.
57
+
58
+ -->
59
+ <def tag="name-one-bootstrap" attrs="complete-target, completer, min-chars">
60
+ <%
61
+ complete_target ||= this_field_reflection.klass
62
+ completer ||= (complete_target.is_a?(Class) ? complete_target : complete_target.class).name_attribute
63
+ begin
64
+ source = polymorphic_url(complete_target, :action => "complete_#{completer}", :routing_type => :path)
65
+ rescue NoMethodError => e
66
+ source = polymorphic_url(complete_target.class, :action => "complete_#{completer}", :routing_type => :path, :id => complete_target._?.id)
67
+ end
68
+ value = name(:no_wrapper => true, :if_present => true)
69
+ -%>
70
+ <typeahead completer-path="&source" data-minLength="&min_chars || 1" merge/>
71
+ </def>
@@ -4,3 +4,8 @@
4
4
  <search-results/>
5
5
  </modal>
6
6
  </def>
7
+
8
+ <!-- alias `<name-one>` to `<name-one-bootstrap>` -->
9
+ <def tag="name-one">
10
+ <name-one-bootstrap merge/>
11
+ </def>
@@ -14,11 +14,8 @@ You also need to modify the create action of the new record. projects_controller
14
14
 
15
15
  def create
16
16
  hobo_create do
17
- if request.xhr?
18
- @this = Story.new(:project => @project)
19
- else
20
- create_response
21
- end
17
+ @this = Story.new(:project => @project) if request.xhr?
18
+ create_response
22
19
  end
23
20
  end
24
21
 
@@ -0,0 +1,50 @@
1
+ <!--
2
+ This is the [Bootstrap typeahead](http://twitter.github.com/bootstrap/javascript.html#typeahead).
3
+
4
+ If you do not provide a `source` option, this tag will essentially provide a default similar to this:
5
+
6
+ <typeahead:project source="&Project.find(:all, :limit => 5000).map {|p| p.name}" />
7
+
8
+ The javascript options supported by Bootstrap typeahead may be provided either as a function name or a javascript snippet. For example, case sensitive match:
9
+
10
+ <typeahead matcher="arguments[0].indexOf(this.query)>=0"/>
11
+
12
+ ### Attributes
13
+
14
+ You may only supply one of `data-source`, `source`, or `completer-path`
15
+
16
+ - `source`: the list of items that may be matched, as a Ruby array.
17
+ - `source-function`: a Javascript function that returns the list of items for a query. See the Bootstrap typeahead documentation for more details
18
+ - `completer-path`: the path to a hobo autocomplete action. See `<name-one>` for more information. In fact, you're better off using `<name-one>` directly.
19
+
20
+ Other attributes:
21
+
22
+ - `items`: The max number of items to display in the dropdown.
23
+ - `minLength`: The minimum character length needed before triggering autocomplete suggestions
24
+ - `placeholder`: the placeholder to display when the input has no value
25
+ - `matcher`, `sorter`, `updater`, `highlighter`: javascript functions
26
+
27
+ -->
28
+ <def tag="typeahead" attrs="nil-value, completer-path, source-function">
29
+ <%
30
+ options, attributes = attributes.partition_hash(%w(minLength items source))
31
+ events, attributes = attributes.partition_hash(%w(source matcher sorter updater highlighter))
32
+
33
+ if !completer_path
34
+ options['source'] ||= source_function || begin
35
+ complete_target = this_field_reflection.klass
36
+ complete_target.find(:all, :limit => 5000).*.send(complete_target.name_attribute)
37
+ end
38
+ end
39
+
40
+
41
+ attributes["name"] ||= param_name_for_this
42
+ attributes["value"] ||= name(:no_wrapper => true, :if_present => true)
43
+
44
+ attributes["placeholder"] ||= nil_value
45
+ add_data_rapid!(attributes, "typeahead", :options => options, :events => events, :completer_path => completer_path)
46
+ %>
47
+ <wrap tag="span" class="field-with-errors" when="&!this_parent.errors[this_field].empty?">
48
+ <input type="text" data-provide="typeahead" merge-attrs/>
49
+ </wrap>
50
+ </def>
@@ -0,0 +1,14 @@
1
+ /* typeahead */
2
+ (function($) {
3
+ $.fn.hjq_typeahead = function(annotations) {
4
+ var opts = this.hjq('getOptions', annotations);
5
+ if(annotations.completer_path) {
6
+ opts.source = function (query, process) {
7
+ return $.get(annotations.completer_path, { term: query }, function (data) {
8
+ return process(data);
9
+ });
10
+ };
11
+ }
12
+ this.typeahead(opts);
13
+ };
14
+ })( jQuery );
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hobo_bootstrap_ui
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.pre6
4
+ version: 2.0.0.pre7
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-15 00:00:00.000000000 Z
12
+ date: 2012-12-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: hobo_bootstrap
@@ -41,9 +41,12 @@ files:
41
41
  - screenshots/select_one_or_new.png
42
42
  - taglibs/hobo_bootstrap_ui.dryml
43
43
  - taglibs/modal.dryml
44
+ - taglibs/name_one_bootstrap.dryml
44
45
  - taglibs/overrides.dryml
45
46
  - taglibs/select_one_or_new.dryml
47
+ - taglibs/typeahead.dryml
46
48
  - vendor/assets/javascripts/hobo-bootstrap-ui/hjq-modal.js
49
+ - vendor/assets/javascripts/hobo-bootstrap-ui/hjq-typeahead.js
47
50
  - vendor/assets/javascripts/hobo_bootstrap_ui.js
48
51
  - vendor/assets/stylesheets/hobo_bootstrap_ui.css
49
52
  homepage: https://github.com/Hobo/hobo_bootstrap_ui