benhoskings-hammock 0.2.25 → 0.3.0

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.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.3.0 2009-05-25
2
+ Updated ajax_link, ajaxinate and related methods to accept explicit args instead of a splatted list - this makes it a lot easier to handle different amounts of entities.
3
+ Only do obj replacement in jquery_xhr callback if it's set.
4
+
1
5
  == 0.2.25 2009-05-25
2
6
  Added String#clean_email[!].
3
7
  Moved set_new_or_deleted_before_save callback from controller to model.
data/hammock.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{hammock}
5
- s.version = "0.2.25"
5
+ s.version = "0.3.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Ben Hoskings"]
data/lib/hammock.rb CHANGED
@@ -4,7 +4,7 @@ require 'ambition'
4
4
  require 'ambition/adapters/active_record'
5
5
 
6
6
  module Hammock
7
- VERSION = '0.2.25'
7
+ VERSION = '0.3.0'
8
8
 
9
9
  def self.included base # :nodoc:
10
10
  puts "Loading Hammock from #{loaded_from_gem? ? 'gem' : 'plugin'}"
@@ -16,25 +16,30 @@ module Hammock
16
16
  ajax_link verb, record, opts.merge(:class => [opts[:class], 'button'].squash.join(' '))
17
17
  end
18
18
 
19
- def ajax_link verb, record, opts = {}
20
- if can_verb_entity?(verb, record)
21
- route = ajaxinate verb, record, opts
19
+ def ajax_link verb, entities, opts
20
+ entities = [entities] unless entities.is_a?(Array)
21
+
22
+ if can_verb_entity?(verb, entities.last)
23
+ route = ajaxinate verb, entities, opts
22
24
 
23
25
  content_tag :a,
24
26
  opts[:text] || route.verb.to_s.capitalize,
25
- :class => [opts[:class], link_class_for(route.verb, record)].squash.join(' '),
27
+ :class => [opts[:class], link_class_for(route.verb, entities)].squash.join(' '),
26
28
  :href => route.path,
27
29
  :onclick => 'return false;',
28
30
  :style => opts[:style]
29
31
  end
30
32
  end
31
33
 
32
- def ajaxinate verb, record, opts = {}
33
- record_attributes = {record.base_model => record.unsaved_attributes}
34
- link_params = {record.base_model => (opts.delete(:record) || {}) }.merge(opts[:params] || {})
35
- route = route_for verb, record
34
+ def ajaxinate verb, entities, opts = {}
35
+ entities = [entities] unless entities.is_a?(Array)
36
+ entity = entities.last
37
+
38
+ record_attributes = entity.is_a?(ActiveRecord::Base) ? {entity.base_model => entity.unsaved_attributes} : {}
39
+ link_params = {entity.base_model => (opts.delete(:record) || {}) }.merge(opts[:params] || {})
40
+ route = route_for verb, *entities
36
41
  attribute = link_params[:attribute]
37
- link_class = link_class_for route.verb, record, attribute
42
+ link_class = link_class_for route.verb, entities, attribute
38
43
 
39
44
  link_params[:_method] = route.http_method
40
45
  link_params[:format] = opts[:format].to_s unless opts[:format].blank?
@@ -44,7 +49,7 @@ module Hammock
44
49
  elsif attribute.blank?
45
50
  "jQuery('form').serializeHash()"
46
51
  else
47
- "{ '#{record.base_model}[#{attribute}]': $('.#{link_class}').val() }"
52
+ "{ '#{entity.base_model}[#{attribute}]': $('.#{link_class}').val() }"
48
53
  end
49
54
 
50
55
  response_action = case link_params[:format].to_s
@@ -115,7 +120,7 @@ module Hammock
115
120
  when 'js'
116
121
  "eval(data);"
117
122
  else
118
- "obj.replaceWith(data);"
123
+ "if (typeof(obj) != 'undefined') { obj.replaceWith(data); }"
119
124
  end
120
125
 
121
126
  %Q{
@@ -141,8 +146,8 @@ module Hammock
141
146
 
142
147
  private
143
148
 
144
- def link_class_for verb, record, attribute = nil
145
- [verb, record.description, attribute].compact.join('_')
149
+ def link_class_for verb, entitites, attribute = nil
150
+ [verb, entitites.map(&:description), attribute].flatten.compact.join '_'
146
151
  end
147
152
 
148
153
  def clean_snippet snippet
@@ -104,7 +104,7 @@ module Hammock
104
104
  @queries = params[:q].downcase.split(/\s+/)
105
105
 
106
106
  mdl.send :with_scope, :find => suggest_scope.to_hash do
107
- mdl.suggest fields, @queries
107
+ mdl.suggest fields, @queries, params[:limit].to_i
108
108
  end
109
109
  end
110
110
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: benhoskings-hammock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.25
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Hoskings