Skip to content

Commit

Permalink
rename, add options to lps typeahead
Browse files Browse the repository at this point in the history
  • Loading branch information
stevedowney committed Jul 8, 2013
1 parent 9b16118 commit 8ff0fbc
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
ucb_rails (0.0.10)
ucb_rails (0.0.12)
active_attr
bootstrap-datepicker-rails
bootstrap-sass (~> 2.3)
Expand Down
4 changes: 3 additions & 1 deletion app/helpers/ucb_rails/lps_typeahead_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ module UcbRails::LpsTypeaheadHelper
# @option options [String] :result_link_text ('Select') the text of the link button in search results
# @option options [String] :result_link_class ('lps-typeahead-item') class to be added the the results link
# @option options [String] :uid_dom_id ('uid') the dom-id of the (hidden) uid <input>
# @option options [String] :search_url ('/ucb_rails/admin/users/typeahead_search') the search url for typeahead
# @option options [String] :typeahead_url ('/ucb_rails/admin/users/typeahead_search') the typeahead url
# @option options [String] :ldap_search_url ('/ucb_rails/ldap_person_search/) the ldap search rul
# @option options [Boolean] :ldap_search (true) include the icon/markup for ldap searches
def lps_typeahead_search_field(options={})
UcbRails::Renderer::LpsTypeaheadSearchField.new(self, options).html
end
Expand Down
32 changes: 23 additions & 9 deletions app/helpers/ucb_rails/renderer/lps_typeahead_search_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Renderer

class LpsTypeaheadSearchField < Base

attr_accessor :name, :label, :required, :value, :placeholder, :hint, :result_link_text, :result_link_class, :uid_dom_id, :search_url
attr_accessor :name, :label, :required, :value, :placeholder, :hint, :result_link_text, :result_link_class, :uid_dom_id, :typeahead_url, :ldap_search_url, :ldap_search

def initialize(template, options={})
super
Expand All @@ -14,10 +14,7 @@ def html
content_tag(:div, class: 'control-group lps-typeahead') do
label_html +
content_tag(:div, class: 'controls') do
content_tag(:div, class: 'input-append') do
text_field_html +
span_html
end +
inputs +
content_tag(:p, hint, class: 'help-block')
end
end
Expand All @@ -35,13 +32,28 @@ def label_html
label
end
end


def inputs
if ldap_search
input_append_div
else
text_field_html
end
end

def input_append_div
content_tag(:div, class: 'input-append') do
text_field_html + span_html
end
end

def text_field_html
text_field_tag(name, value, {
autocomplete: 'off',
class: 'typeahead-lps-search',
placeholder: placeholder,
data: {
url: typeahead_url,
uid_dom_id: uid_dom_id,
}
})
Expand All @@ -51,7 +63,7 @@ def span_html
span_options = {
class: 'add-on ldap-person-search',
data: {
search_url: search_url,
url: ldap_search_url,
search_field_name: name,
result_link_text: result_link_text,
result_link_class: result_link_class
Expand All @@ -72,7 +84,9 @@ def parse_options
self.result_link_text = options.delete(:result_link_text) || 'Select'
self.result_link_class = options.delete(:result_link_class) || 'lps-typeahead-item'
self.uid_dom_id = options.delete(:uid_dom_id) || 'uid'
self.search_url = options.delete(:search_url) || typeahead_search_ucb_rails_admin_users_path
self.typeahead_url = options.delete(:typeahead_url) || typeahead_search_ucb_rails_admin_users_path
self.ldap_search_url = options.delete(:ldap_search_url) || ldap_search_ucb_rails_admin_users_path
self.ldap_search = options.delete(:ldap_search) != false

validate_options
end
Expand All @@ -81,7 +95,7 @@ def validate_options
return if options.blank?

msg = "Unknown lps_typeahead_search_field option(s): #{options.keys.map(&:inspect).join(', ')}. "
msg << "Did you mean one of :name, :required, :label, :value, :placeholder, :hint, :result_link_text, :result_link_class, :uid_dom_id, :search_url"
msg << "Did you mean one of :name, :required, :label, :value, :placeholder, :hint, :ldap_search, :ldap_search_url, :result_link_text, :result_link_class, :uid_dom_id, :typeahead_url"
raise ArgumentError, msg
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ $(function() {
}

function setSearchUrl(link) {
var url = link.data('search-url');
var url = link.data('url');
var formAction = url == undefined ? '/ucb_rails/ldap_person_search' : url;
$('form#lps-form').attr('action', formAction);
}
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/assets/javascripts/ldap_person_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ $(function() {
}

function setSearchUrl(link) {
var url = link.data('search-url');
var url = link.data('url');
var formAction = url == undefined ? '/ucb_rails/ldap_person_search' : url;
$('form#lps-form').attr('action', formAction);
}
Expand Down
36 changes: 32 additions & 4 deletions spec/helpers/ucb_rails/lps_typeahead_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
div_ia.find('input.typeahead-lps-search').tap do |input|
input['autocomplete'].should == 'off'
input['data-uid-dom-id'].should == 'uid'
input['data-url'].should == '/ucb_rails/admin/users/typeahead_search'
input['id'].should == 'person_search'
input['name'].should == 'person_search'
input['placeholder'].should == 'Type name to search'
input['type'].should == 'text'
end
end
div_c.find('span.add-on.ldap-person-search').tap do |span|
span['data-search-url'].should == '/ucb_rails/admin/users/typeahead_search'
span['data-url'].should == '/ucb_rails/admin/users/ldap_search'
span['data-result-link-class'].should == 'lps-typeahead-item'
span['data-result-link-text'].should == 'Select'
span['data-search-field-name'].should == 'person_search'
Expand Down Expand Up @@ -115,12 +116,39 @@
end
end

it "search url" do
html = helper.lps_typeahead_search_field(search_url: '/surch')
it "typeahead url" do
html = helper.lps_typeahead_search_field(typeahead_url: '/ta_url')
Capybara.string(html).find('div.control-group.lps-typeahead').tap do |div_cg|
div_cg.find('input[data-url="/ta_url"]')
end
end

it "ldap search url" do
html = helper.lps_typeahead_search_field(ldap_search_url: '/ls_url')
Capybara.string(html).find('div.control-group.lps-typeahead').tap do |div_cg|
div_cg.find('span[data-url="/ls_url"]')
end
end

it "no ldap search" do
html = helper.lps_typeahead_search_field(ldap_search: false)

Capybara.string(html).find('div.control-group.lps-typeahead').tap do |div_cg|
div_cg.find('span[data-search-url="/surch"]')
div_cg.find('label.control-label[for="person_search"]', text: 'User')
div_cg.find('div.controls').tap do |div_c|
div_c.find('input.typeahead-lps-search').tap do |input|
input['autocomplete'].should == 'off'
input['data-uid-dom-id'].should == 'uid'
input['data-url'].should == '/ucb_rails/admin/users/typeahead_search'
input['id'].should == 'person_search'
input['name'].should == 'person_search'
input['placeholder'].should == 'Type name to search'
input['type'].should == 'text'
end
div_c.find('p.help-block', text: 'Click icon to search CalNet')
end
end

end

it "unknown option" do
Expand Down

0 comments on commit 8ff0fbc

Please sign in to comment.