resource_awareness 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ module Rails
2
+ class Resource
3
+
4
+ attr_accessor :name, :singular_name, :id, :route, :controller_name, :controller, :name_prefix
5
+
6
+ def initialize(entity, scope, options)
7
+ self.name_prefix = scope[:name_prefix]
8
+ self.name = entity.to_s
9
+ self.singular_name = name.singularize
10
+ 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
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ module Rails
2
+ class SingletonResource < Resource
3
+
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module ResourceAwareness
2
+ require 'resource_awareness/railtie' if defined?(Rails)
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'action_controller/base'
2
+
3
+ class ActionController::Base
4
+ class << self
5
+
6
+ def resource
7
+ @resource ||= Rails.application.resources.find { |r| r.controller == self }
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ require 'rails/application'
2
+
3
+ module ResourceAwareness
4
+ module ApplicationExtensions
5
+
6
+ def resources
7
+ @resources ||= []
8
+ end
9
+
10
+ end
11
+ end
12
+
13
+ Rails::Application.send(:include, ResourceAwareness::ApplicationExtensions)
@@ -0,0 +1,37 @@
1
+ require 'action_dispatch/routing/mapper'
2
+ require 'rails/resource'
3
+ require 'rails/singleton_resource'
4
+
5
+ module ResourceAwareness
6
+ module MapperExtensions
7
+
8
+ def resources(*entities, &block)
9
+ super
10
+ options = entities.extract_options!
11
+ return unless leaf_call?(entities, options)
12
+ Rails.application.resources << Rails::Resource.new(entities.first, @scope, options)
13
+ end
14
+
15
+ def resource(*entities, &block)
16
+ super
17
+ options = entities.extract_options!
18
+ return unless leaf_call?(entities, options)
19
+ Rails.application.resources << Rails::SingletonResource.new(entities.first, @scope, options)
20
+ end
21
+
22
+ private
23
+
24
+ # the resource(s) methods call themselves recursively in certain conditions
25
+ # (see the Mapper#apply_common_behavior_for method for details)
26
+ # this method returns true only if this is a 'leaf call' (a call that doesn't call itself again)
27
+ 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
32
+ end
33
+
34
+ end
35
+ end
36
+
37
+ ActionDispatch::Routing::Mapper.send(:include, ResourceAwareness::MapperExtensions)
@@ -0,0 +1,24 @@
1
+ require 'resource_awareness'
2
+ require 'rails'
3
+
4
+ module ResourceAwareness
5
+ class Railtie < Rails::Railtie
6
+
7
+ initializer 'resource_awareness.extend_application' do
8
+ require 'resource_awareness/application_extensions'
9
+ end
10
+
11
+ initializer 'resource_awareness.extend_mapper' do
12
+ require 'resource_awareness/mapper_extensions'
13
+ end
14
+
15
+ initializer 'resource_awareness.extend_action_controller' do
16
+ require 'resource_awareness/action_controller_extensions'
17
+ end
18
+
19
+ rake_tasks do
20
+ load 'tasks/resource_awareness.rake'
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,4 @@
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)', '')] }
4
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resource_awareness
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 3
9
+ version: 0.0.3
10
+ platform: ruby
11
+ authors:
12
+ - Ingo Weiss
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-21 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rails
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 3
29
+ - 0
30
+ version: "3.0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ description: |
34
+ Makes a Rails application's resources (as defined via the 'resource(s)' routing DSL methods) available as Rails.application.resources
35
+
36
+ email: ingo@ingoweiss.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - lib/rails/resource.rb
45
+ - lib/rails/singleton_resource.rb
46
+ - lib/resource_awareness/action_controller_extensions.rb
47
+ - lib/resource_awareness/application_extensions.rb
48
+ - lib/resource_awareness/mapper_extensions.rb
49
+ - lib/resource_awareness/railtie.rb
50
+ - lib/resource_awareness.rb
51
+ - lib/tasks/resource_awareness.rake
52
+ has_rdoc: true
53
+ homepage: http://www.ingoweiss.com
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ requirements: []
76
+
77
+ rubyforge_project: "[none]"
78
+ rubygems_version: 1.3.6
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: Make Rails aware of it's resources
82
+ test_files: []
83
+