Module: Hammock::Ajaxinate::InstanceMethods

Defined in:
lib/hammock/ajaxinate.rb

Instance Method Summary collapse

Instance Method Details

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



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

def ajax_button verb, record, opts = {}
  ajax_link verb, record, 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, record, opts = {}
  if can_verb_entity?(verb, record)
    route = ajaxinate verb, record, opts

     :a,
      opts[:text] || route.verb.to_s.capitalize,
      :class => [opts[:class], link_class_for(route.verb, record)].squash.join(' '),
      :href => route.path,
      :onclick => 'return false;',
      :style => opts[:style]
  end
end

#ajaxinate(verb, record, 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
56
57
58
59
60
61
62
63
64
65
66
67
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
# File 'lib/hammock/ajaxinate.rb', line 32

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

  link_params[:_method] = route.http_method
  link_params[:format] = opts[:format].to_s unless opts[:format].blank?

  form_elements_hash = if route.get?
    '{ }'
  elsif attribute.blank?
    "jQuery('form').serializeHash()"
  else
    "{ '#{record.base_model}[#{attribute}]': $('.#{link_class}').val() }"
  end

  response_action = case link_params[:format].to_s
  when 'js'
    "eval(response);"
  else
    "jQuery('.#{opts[:target] || link_class + '_target'}').before(response).remove();"
  end

  # TODO check the response code in the callback, and replace :after with :success and :failure.
  js = %Q{
    jQuery('.#{link_class}').#{opts[:on] || 'click'}(function() {
      var obj = null;

      if (#{opts[:spinner] ? 'true' : 'false'}) {
        obj = jQuery(this).parent().find('.statuses');
        obj.find('.spinner').animate({opacity: 'show'}, 100);
      }

      /*if (#{attribute.blank? ? 'false' : 'true'} && (jQuery('.#{link_class}_target .original_value').html() == jQuery('.#{link_class}_target .modify input').val())) {
        eval("#{clean_snippet opts[:skipped]}");
      } else*/ if (false == eval("#{clean_snippet opts[:before]}")) {
        // before callback failed
      } else { // fire the request
        jQuery.#{route.fake_http_method}(
          '#{route.path}',
          jQuery.extend(
            #{record_attributes.to_flattened_json},
            #{form_elements_hash},
            #{link_params.to_flattened_json},
            #{forgery_key_json(route.http_method)}
          ),
          function(response, textStatus) {
            #{response_action}
            if (obj) {
              obj.children('.spinner').hide();
              #{"if ('success' == textStatus) obj.children('.success').show().fadeOut(4000);" if opts[:spinner] != :pending}
            }
            eval("#{clean_snippet opts[:after]}");
          }
        );
      }
    });
  }

  append_javascript js
  route
end

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/hammock/ajaxinate.rb', line 108

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
    "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)}
      ),
      function(data, textStatus) {
        #{response_action}
        #{status_callback}
        #{(opts[:callback] || '').end_with(';')}
      }
    );
  }
end

#status_callbackObject



97
98
99
100
101
102
103
104
105
106
# File 'lib/hammock/ajaxinate.rb', line 97

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