Module: Hammock::Ajaxinate::InstanceMethods

Defined in:
lib/hammock/ajaxinate.rb

Instance Method Summary collapse

Instance Method Details

#ajax_button(verb, entities, opts = {}) ⇒ Object



15
16
17
# File 'lib/hammock/ajaxinate.rb', line 15

def ajax_button verb, entities, opts = {}
  ajax_link verb, entities, opts.merge(:class => [opts[:class], 'button'].squash.join(' '))
end


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hammock/ajaxinate.rb', line 19

def ajax_link verb, entities, opts = {}
  if can_verb_entity?(verb, *entities)
    route = ajaxinate verb, entities, opts

     :a,
      opts[:text] || route.verb.to_s.capitalize,
      :class => ['hammock', opts[:class], link_class_for(route.verb, entities), "hammock_link_#{link_class_for(route.verb, entities)}"].squash.join(' '),
      :href => route.path,
      :onclick => 'return false;',
      :style => opts[:style]
  end
end

#ajaxinate(verb, entities, opts = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hammock/ajaxinate.rb', line 32

def ajaxinate verb, entities, opts = {}
  entity = [*entities].last
  record_attributes = entity.is_a?(ActiveRecord::Base) ? {entity.base_model => entity.unsaved_attributes} : {}
  link_params = {entity.base_model => (opts.delete(:record) || {}) }.merge(opts[:params] || {})
  route = route_for verb, *entities
  attribute = link_params[:attribute]
  link_class = link_class_for route.verb, entities, attribute

  link_data = {
    :klass => link_class,
    :path => route.path,
    :http_method => route.http_method,
    :fake_http_method => route.fake_http_method,
    :resource => entity.base_model,
    :attribute => link_params[:attribute],
    :record_attributes => record_attributes.to_param_hash,
    :passed_attributes => link_params.to_param_hash,
    :forgery_key => forgery_key_json(route.http_method),
    :format => (opts[:format].blank? ? 'html' : opts[:format].to_s)
  }

  append_javascript %Q{hammock_link_map['#{link_class}'] = #{link_data.to_json};}
  route
end

#jquery_xhr(verb, record, opts = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/hammock/ajaxinate.rb', line 68

def jquery_xhr verb, record, opts = {}
  route = route_for verb, record
  params = if opts[:params].is_a?(String)
    opts[:params].chomp(',').start_with('{').end_with('}')
  else
    (opts[:params] || {}).merge(record.base_model => (opts[:record] || {})).to_flattened_json
  end

  response_action = case opts[:format].to_s
  when 'js'
    "eval(data);"
  else
    "if (typeof(obj) != 'undefined') { obj.replaceWith(data); }"
  end

  %Q{
    if (typeof(obj) != 'undefined') {
      jQuery('.spinner', obj).show();
    }

    jQuery.#{route.fake_http_method}(
      '#{route.path}',
      jQuery.extend(
        #{params},
        {format: '#{opts[:format] || 'html'}', _method: '#{route.http_method}'},
        #{forgery_key_json(route.http_method).to_flattened_json}
      ),
      function(data, textStatus) {
        #{response_action}
        #{status_callback}
        #{(opts[:callback] || '').end_with(';')}
      }
    );
  }
end

#status_callbackObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/hammock/ajaxinate.rb', line 57

def status_callback
  %Q{
    if ('success' == textStatus) {
      jQuery('.success', jQuery(data)).show().fadeOut(4000);
    } else {
      jQuery('.statuses .failure', obj).hide();
      jQuery('.statuses .failure', obj).show().parents('obj').BlindUp();
    }
  }
end