hammock 0.3.1 → 0.3.2

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,8 +1,12 @@
1
- == 0.3.1 2009-05-26
2
- Check for a specific scope for the current action before checking for a default-named scope.
3
- Commented ambition query logging.
4
- Moved index rendering to restful_rendering.
5
- Wrap pending -> success notification within obj null test.
1
+ == 0.3.2 2009-05-28
2
+ Accidentally pushed 0.3.1 in an unknown state - bumped to 0.3.2 to be safe.
3
+
4
+
5
+ == 0.3.1 2009-05-28
6
+ Added Hammock.load_models, along with call in draw_with_hammock_route_map_init.
7
+ Refactored Hammock.included out into load_hammock_components and related methods, and made them all private.
8
+ Only extract record_attributes from records - not resources too.
9
+ Fixed http status entry in log_hit.
6
10
 
7
11
 
8
12
  == 0.3.0 2009-05-25
data/hammock.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{hammock}
5
- s.version = "0.3.1"
5
+ s.version = "0.3.2"
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"]
9
- s.date = %q{2009-05-26}
9
+ s.date = %q{2009-05-28}
10
10
  s.description = %q{Hammock is a Rails plugin that eliminates redundant code in a very RESTful manner. It does this in lots in lots of different places, but in one manner: it encourages specification in place of implementation.
11
11
 
12
12
 
@@ -16,30 +16,25 @@ 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, 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
19
+ def ajax_link verb, record, opts = {}
20
+ if can_verb_entity?(verb, record)
21
+ route = ajaxinate verb, record, opts
24
22
 
25
23
  content_tag :a,
26
24
  opts[:text] || route.verb.to_s.capitalize,
27
- :class => [opts[:class], link_class_for(route.verb, entities)].squash.join(' '),
25
+ :class => [opts[:class], link_class_for(route.verb, record)].squash.join(' '),
28
26
  :href => route.path,
29
27
  :onclick => 'return false;',
30
28
  :style => opts[:style]
31
29
  end
32
30
  end
33
31
 
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
32
+ def ajaxinate verb, record, opts = {}
33
+ record_attributes = record.is_a?(ActiveRecord::Base) ? {record.base_model => record.unsaved_attributes} : {}
34
+ link_params = {record.base_model => (opts.delete(:record) || {}) }.merge(opts[:params] || {})
35
+ route = route_for verb, record
41
36
  attribute = link_params[:attribute]
42
- link_class = link_class_for route.verb, entities, attribute
37
+ link_class = link_class_for route.verb, record, attribute
43
38
 
44
39
  link_params[:_method] = route.http_method
45
40
  link_params[:format] = opts[:format].to_s unless opts[:format].blank?
@@ -49,7 +44,7 @@ module Hammock
49
44
  elsif attribute.blank?
50
45
  "jQuery('form').serializeHash()"
51
46
  else
52
- "{ '#{entity.base_model}[#{attribute}]': $('.#{link_class}').val() }"
47
+ "{ '#{record.base_model}[#{attribute}]': $('.#{link_class}').val() }"
53
48
  end
54
49
 
55
50
  response_action = case link_params[:format].to_s
@@ -84,10 +79,8 @@ module Hammock
84
79
  ),
85
80
  function(response, textStatus) {
86
81
  #{response_action}
87
- if (obj) {
88
- obj.children('.spinner').hide();
89
- #{"if ('success' == textStatus) obj.children('.success').show().fadeOut(4000);" if opts[:spinner] != :pending}
90
- }
82
+ if (obj) obj.children('.spinner').hide();
83
+ #{"if ('success' == textStatus) obj.children('.success').show().fadeOut(4000);" if opts[:spinner] != :pending}
91
84
  eval("#{clean_snippet opts[:after]}");
92
85
  }
93
86
  );
@@ -122,7 +115,7 @@ module Hammock
122
115
  when 'js'
123
116
  "eval(data);"
124
117
  else
125
- "if (typeof(obj) != 'undefined') { obj.replaceWith(data); }"
118
+ "obj.replaceWith(data);"
126
119
  end
127
120
 
128
121
  %Q{
@@ -148,8 +141,8 @@ module Hammock
148
141
 
149
142
  private
150
143
 
151
- def link_class_for verb, entitites, attribute = nil
152
- [verb, entitites.map(&:description), attribute].flatten.compact.join '_'
144
+ def link_class_for verb, record, attribute = nil
145
+ [verb, record.description, attribute].compact.join('_')
153
146
  end
154
147
 
155
148
  def clean_snippet snippet
@@ -19,7 +19,7 @@ module Hammock
19
19
  (@current_site.subdomain unless @current_site.nil?),
20
20
  (request.session_options[:id].nil? ? 'nil' : ('...' + request.session_options[:id][-8, 8])),
21
21
  (current_user.nil? ? "unauthed" : "Account<#{current_user.id}> #{current_user.name}").colorize('green'),
22
- headers['Status'],
22
+ response.status,
23
23
  log_hit_request_info,
24
24
  log_hit_route_info
25
25
  ].squash.join(' | ')
@@ -6,7 +6,14 @@ module Hammock
6
6
  #
7
7
  # Lists the current resource's records that are visible within the current index scope, defined by +index_scope+ and +index_scope_for+ on the current model.
8
8
  def index
9
- render_for_index if tasks_for_index
9
+ if tasks_for_index
10
+ respond_to do |format|
11
+ format.html
12
+ format.xml { render :xml => @records.kick }
13
+ format.json { render :json => @records.kick }
14
+ format.yaml { render :text => @records.kick.to_yaml }
15
+ end
16
+ end
10
17
  end
11
18
 
12
19
  # The +new+ action. (GET, safe, idempotent)
@@ -28,15 +28,6 @@ module Hammock
28
28
  end
29
29
  end
30
30
 
31
- def render_for_index
32
- respond_to do |format|
33
- format.html
34
- format.xml { render :xml => @records.kick }
35
- format.json { render :json => @records.kick }
36
- format.yaml { render :text => @records.kick.to_yaml }
37
- end
38
- end
39
-
40
31
  def render_for_safe_actions result = true, opts = {}
41
32
  if request.xhr?
42
33
  if params[:attribute]
@@ -77,7 +77,7 @@ module Hammock
77
77
  elsif record_or_records.is_a? ActiveRecord::Base
78
78
  instance_variable_set "@#{mdl_name}", (@record = record_or_records)
79
79
  elsif record_or_records.is_a? Ambition::Context
80
- # log "Unkicked query: #{record_or_records.to_hash.inspect}"
80
+ log "Unkicked query: #{record_or_records.to_hash.inspect}"
81
81
  instance_variable_set "@#{mdl_name.pluralize}", (@records = record_or_records)
82
82
  elsif record_or_records.is_a? Array
83
83
  instance_variable_set "@#{mdl_name.pluralize}", (@records = record_or_records)
@@ -13,6 +13,7 @@ module Hammock
13
13
  module Methods
14
14
 
15
15
  def draw_with_hammock_route_map_init &block
16
+ Hammock.load_models
16
17
  ActionController::Routing::Routes.send :initialize_hammock_route_map
17
18
  draw_without_hammock_route_map_init &block
18
19
  end
data/lib/hammock/scope.rb CHANGED
@@ -116,10 +116,10 @@ module Hammock
116
116
  end
117
117
 
118
118
  def account_verb_scope?
119
- mdl.has_account_scope?(action_name) || mdl.has_account_scope?(scope_name_for_action)
119
+ mdl.has_account_scope? scope_name_for_action
120
120
  end
121
121
  def public_verb_scope?
122
- mdl.has_public_scope?(action_name) || mdl.has_public_scope?(scope_name_for_action)
122
+ mdl.has_public_scope? scope_name_for_action
123
123
  end
124
124
 
125
125
  end
data/lib/hammock.rb CHANGED
@@ -4,15 +4,47 @@ require 'ambition'
4
4
  require 'ambition/adapters/active_record'
5
5
 
6
6
  module Hammock
7
- VERSION = '0.3.1'
7
+ VERSION = '0.3.2'
8
8
 
9
9
  def self.included base # :nodoc:
10
10
  puts "Loading Hammock from #{loaded_from_gem? ? 'gem' : 'plugin'}"
11
+ load_hammock_components base
12
+ end
13
+
14
+ def self.loaded_from_gem?
15
+ File.dirname(__FILE__)[`gem env gemdir`.chomp]
16
+ end
17
+
18
+ # Trigger a load of every model by trying to access the class we expect in
19
+ # each file within app/models. This ensures ActiveRecord::Base.subclasses
20
+ # and related methods always return the full set of models they should.
21
+ def self.load_models
22
+ Dir.glob(RAILS_ROOT / 'app/models/**/*.rb').each {|model_file|
23
+ klass = File.basename(model_file, '.rb').classify
24
+ begin
25
+ Object.const_get klass
26
+ puts "Loaded #{klass} from #{model_file}."
27
+ rescue
28
+ puts "Couldn't load #{klass} from #{model_file}."
29
+ end
30
+ }
31
+ end
11
32
 
33
+
34
+ private
35
+
36
+ def self.load_hammock_components base
37
+ require_hammock_components
38
+ mixin_hammock_components base
39
+ end
40
+
41
+ def self.require_hammock_components
12
42
  Dir.glob("#{File.dirname __FILE__}/hammock/**/*.rb").each {|dep|
13
43
  require dep
14
44
  }
45
+ end
15
46
 
47
+ def self.mixin_hammock_components base
16
48
  Hammock.constants.map {|constant_name|
17
49
  Hammock.const_get constant_name
18
50
  }.select {|constant|
@@ -25,9 +57,6 @@ module Hammock
25
57
  }
26
58
  end
27
59
 
28
- def self.loaded_from_gem?
29
- File.dirname(__FILE__)[`gem env gemdir`.chomp]
30
- end
31
60
  end
32
61
 
33
62
  # This is done in init.rb when Hammock is loaded as a plugin.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hammock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Hoskings
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-26 00:00:00 +10:00
12
+ date: 2009-05-28 00:00:00 +10:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency