resource_awareness 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,17 +1,35 @@
1
1
  module Rails
2
2
  class Resource
3
-
4
- attr_accessor :name, :singular_name, :id, :route, :controller_name, :controller, :name_prefix
5
-
3
+ attr_accessor :id, :name, :singular_name, :controller_name, :name_prefix, :path_prefix, :home_route
4
+
6
5
  def initialize(entity, scope, options)
7
6
  self.name_prefix = scope[:name_prefix]
8
7
  self.name = entity.to_s
9
8
  self.singular_name = name.singularize
10
9
  self.id = [name_prefix, name].compact.join('_')
11
- self.route = Rails.application.routes.named_routes[id]
12
- self.controller_name = route.defaults[:controller]
13
- self.controller = (controller_name + '_controller').camelize.constantize
10
+ self.home_route = Rails.application.routes.named_routes[id]
11
+ self.path_prefix = home_route.path.sub("/#{name}(.:format)", '')
12
+ self.path_prefix = nil if path_prefix.blank?
13
+ self.controller_name = home_route.requirements[:controller]
14
+ end
15
+
16
+ def path
17
+ path_prefix ? "#{path_prefix}/#{name}" : "/#{name}"
14
18
  end
15
19
 
20
+ def attributes
21
+ {
22
+ :id => id,
23
+ :name => name,
24
+ :singular_name => singular_name,
25
+ :path_prefix => path_prefix,
26
+ :path => path,
27
+ :controller_name => controller_name
28
+ }
29
+ end
30
+
31
+ def to_xml
32
+ attributes.to_xml
33
+ end
16
34
  end
17
35
  end
@@ -1,5 +1,4 @@
1
1
  module Rails
2
2
  class SingletonResource < Resource
3
-
4
3
  end
5
4
  end
@@ -1,11 +1,7 @@
1
1
  require 'action_controller/base'
2
2
 
3
3
  class ActionController::Base
4
- class << self
5
-
6
- def resource
7
- @resource ||= Rails.application.resources.find { |r| r.controller == self }
8
- end
9
-
4
+ def resource
5
+ @resource ||= Rails.application.resources.find { |r| r.controller_name == self.controller_name }
10
6
  end
11
7
  end
@@ -2,11 +2,9 @@ require 'rails/application'
2
2
 
3
3
  module ResourceAwareness
4
4
  module ApplicationExtensions
5
-
6
5
  def resources
7
6
  @resources ||= []
8
7
  end
9
-
10
8
  end
11
9
  end
12
10
 
@@ -4,33 +4,27 @@ require 'rails/singleton_resource'
4
4
 
5
5
  module ResourceAwareness
6
6
  module MapperExtensions
7
-
8
7
  def resources(*entities, &block)
9
8
  super
10
9
  options = entities.extract_options!
11
- return unless leaf_call?(entities, options)
12
- Rails.application.resources << Rails::Resource.new(entities.first, @scope, options)
10
+ Rails.application.resources << Rails::Resource.new(entities.first, @scope, options) if leaf_call?(entities, options)
13
11
  end
14
-
12
+
15
13
  def resource(*entities, &block)
16
14
  super
17
15
  options = entities.extract_options!
18
- return unless leaf_call?(entities, options)
19
- Rails.application.resources << Rails::SingletonResource.new(entities.first, @scope, options)
16
+ Rails.application.resources << Rails::SingletonResource.new(entities.first, @scope, options) if leaf_call?(entities, options)
20
17
  end
21
-
18
+
22
19
  private
23
-
20
+
24
21
  # the resource(s) methods call themselves recursively in certain conditions
25
22
  # (see the Mapper#apply_common_behavior_for method for details)
26
23
  # this method returns true only if this is a 'leaf call' (a call that doesn't call itself again)
24
+ # TODO: there should be a better way to determine this
27
25
  def leaf_call?(entities, options)
28
- return false if @scope[:scope_level] == :resources
29
- return false if entities.length > 1
30
- return false if options[:path_names]
31
- true
26
+ !(@scope[:scope_level] == :resources || entities.length > 1 || options[:path_names])
32
27
  end
33
-
34
28
  end
35
29
  end
36
30
 
@@ -3,22 +3,22 @@ require 'rails'
3
3
 
4
4
  module ResourceAwareness
5
5
  class Railtie < Rails::Railtie
6
-
6
+
7
7
  initializer 'resource_awareness.extend_application' do
8
8
  require 'resource_awareness/application_extensions'
9
9
  end
10
-
10
+
11
11
  initializer 'resource_awareness.extend_mapper' do
12
12
  require 'resource_awareness/mapper_extensions'
13
13
  end
14
-
14
+
15
15
  initializer 'resource_awareness.extend_action_controller' do
16
16
  require 'resource_awareness/action_controller_extensions'
17
17
  end
18
-
18
+
19
19
  rake_tasks do
20
20
  load 'tasks/resource_awareness.rake'
21
21
  end
22
-
22
+
23
23
  end
24
24
  end
@@ -1,4 +1,4 @@
1
1
  task :resources => :environment do
2
- widest_route_id = Rails.application.resources.collect(&:id).max
3
- puts Rails.application.resources.collect { |r| "%-#{widest_route_id.length + 20}s%s" % [r.id, r.route.path.sub('(.:format)', '')] }
2
+ width_of_widest_route_id = Rails.application.resources.collect(&:id).max.length
3
+ puts Rails.application.resources.collect { |r| "%-#{width_of_widest_route_id + 20}s%s" % [r.id, r.path] }
4
4
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ingo Weiss
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-21 00:00:00 +02:00
17
+ date: 2010-04-25 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -31,7 +31,7 @@ dependencies:
31
31
  type: :runtime
32
32
  version_requirements: *id001
33
33
  description: |
34
- Makes a Rails application's resources (as defined via the 'resource(s)' routing DSL methods) available as Rails.application.resources
34
+ Makes information about a Rails application's resources (as defined via the 'resource(s)' routing DSL methods) available via Rails.application.resources
35
35
 
36
36
  email: ingo@ingoweiss.com
37
37
  executables: []
@@ -74,10 +74,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
74
  version: "0"
75
75
  requirements: []
76
76
 
77
- rubyforge_project: "[none]"
77
+ rubyforge_project:
78
78
  rubygems_version: 1.3.6
79
79
  signing_key:
80
80
  specification_version: 3
81
- summary: Make Rails aware of it's resources
81
+ summary: Makes Rails applications aware of their resources
82
82
  test_files: []
83
83