requested 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest ADDED
@@ -0,0 +1,7 @@
1
+ README.textile
2
+ Rakefile
3
+ init.rb
4
+ lib/requested.rb
5
+ lib/requested/action.rb
6
+ requested.gemspec
7
+ Manifest
data/README.textile ADDED
@@ -0,0 +1,124 @@
1
+ h1. Requested
2
+
3
+ ActionController::Request improvements with several methods to get info on requested actions and resources.
4
+
5
+ h2. Use It
6
+
7
+ h3. In your controller
8
+
9
+ <pre>
10
+ if request.action.collection?
11
+ @collection = request.resource.all
12
+
13
+ elsif request.action.single_object?
14
+ @object = request.resource.find params[:id]
15
+
16
+ end
17
+ </pre>
18
+
19
+ h3. In your views
20
+
21
+ <pre>
22
+ <% if request.action.new_or_create? %>
23
+ <%= f.input :image, :required => true %>
24
+ <% else %>
25
+ <%= f.input :image, :required => true, :hint => button('crop', crop_image_path(@object)) %>
26
+ <% end %>
27
+ </pre>
28
+
29
+ h2. Installation
30
+
31
+ h3. As a gem:
32
+
33
+ Add this line to your @environment.rb@:
34
+
35
+ <pre>
36
+ config.gem "requested", :source => 'http://gems.github.com'
37
+ </pre>
38
+
39
+ and then do
40
+
41
+ <pre>
42
+ rake gems:install
43
+ </pre>
44
+
45
+ or just
46
+
47
+ <pre>
48
+ gem install requested --source='http://gems.github.com'
49
+ </pre>
50
+
51
+
52
+ h3. As a plugin
53
+
54
+ <pre>
55
+ script/plugin install git://github.com/mcasimir/requested.git
56
+ </pre>
57
+
58
+
59
+ h2. Usage
60
+
61
+ h3. Test if user agent is a robot
62
+
63
+ <pre>
64
+ request.is_robot?
65
+ </pre>
66
+
67
+ h3. Controller and Action names
68
+
69
+ <pre>
70
+ request.controller
71
+ request.action
72
+ </pre>
73
+
74
+
75
+ h3. Action details
76
+
77
+ <pre>
78
+ # Test if action is one of index, new, create, edit, update, destroy, show:
79
+ request.action.rest?
80
+
81
+ # Test single restful actions
82
+ request.action.index?
83
+ request.action.new?
84
+ request.action.create?
85
+ request.action.edit?
86
+ request.action.update?
87
+ request.action.destroy?
88
+
89
+ # Test if action is for a collection (index) or for a single resource:
90
+ request.collection?
91
+ request.single_object?
92
+
93
+ # Creating resources
94
+ request.new_or_create?
95
+
96
+ # Updating resources
97
+ request.edit_or_update?
98
+ </pre>
99
+
100
+ h3. Resource
101
+
102
+ <pre>
103
+ # Retrieve the resource model class belonging to current controller
104
+ # eg. ArticlesController --> Article
105
+
106
+ request.resource
107
+
108
+
109
+ # Retrieve the name of the resource belonging to current controller
110
+ # eg. ArticlesController --> "article"
111
+
112
+ request.resource_name
113
+
114
+
115
+ # Retrieve the collection name of the resource belonging to current controller
116
+ # eg. ArticlesController --> "articles"
117
+
118
+ request.collection_name
119
+ </pre>
120
+
121
+ h2. Project Details
122
+
123
+ Copyright © 2010 Maurizio Casimirri, released under the LGPL license.
124
+
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('requested', '0.1.1') do |p|
6
+ p.description = "ActionController::Request improvements with several methods to get info on requested actions"
7
+ p.url = "http://github.com/mcasimir/requested"
8
+ p.author = "Maurizio Casimirri"
9
+ p.email = "maurizio.cas@gmail.com"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
15
+
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'requested'
2
+
data/lib/requested.rb ADDED
@@ -0,0 +1,75 @@
1
+ # Author:: Maurizio Casimirri (mailto:maurizio.cas@gmail.com)
2
+ # Copyright:: Copyright (c) 2010 Maurizio Casimirri
3
+
4
+ # This library is free software; you can redistribute it and/or
5
+ # modify it under the terms of the GNU Lesser General Public
6
+ # License as published by the Free Software Foundation; either
7
+ # version 2.1 of the License, or (at your option) any later version.
8
+
9
+ # This library is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # Lesser General Public License for more details.
13
+
14
+ # You should have received a copy of the GNU Lesser General Public
15
+ # License along with this library; if not, write to the Free Software
16
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
+
18
+ module Requested
19
+
20
+ def self.included(base)
21
+ base.extend ClassMethods
22
+ base.send :include, InstanceMethods
23
+ end
24
+
25
+ module InstanceMethods
26
+
27
+ def is_robot?()
28
+ self.user_agent =~ /\b(Baidu|Gigabot|Googlebot|libwww-perl|lwp-trivial|msnbot|SiteUptime|Slurp|WordPress|ZIBB|ZyBorg)\b/i
29
+ end
30
+
31
+ def robot?()
32
+ self.is_robot?
33
+ end
34
+
35
+ def action()
36
+ @requested_action ||= Requested::Action.new(path_parameters['action'])
37
+ end
38
+
39
+ def controller()
40
+ path_parameters['controller']
41
+ end
42
+
43
+ def query_params()
44
+ @query_params ||= request_parameters.dup.delete_if {|k,v| [:action, :controller, :url, :source, :authenticity_token].include?(k.to_sym)}
45
+ end
46
+
47
+ def resource
48
+ @resource ||= controller.gsub(/Controller$/, "").singularize.camelize.constantize rescue nil
49
+ end
50
+
51
+ def resource_name
52
+ @resource_name ||= self.resource.name.tableize.singularize rescue ""
53
+ end
54
+
55
+ def collection_name
56
+ @collection_name ||= self.resource_name.pluralize
57
+ end
58
+
59
+ end
60
+
61
+ module ClassMethods
62
+
63
+ def self.build_query_string(params)
64
+ params.map{|k,v| "#{k}=#{v}"}.join("&")
65
+ end
66
+
67
+ end
68
+
69
+ end
70
+
71
+
72
+ class ActionController::Request
73
+ include Requested
74
+ end
75
+
@@ -0,0 +1,69 @@
1
+ # Author:: Maurizio Casimirri (mailto:maurizio.cas@gmail.com)
2
+ # Copyright:: Copyright (c) 2010 Maurizio Casimirri
3
+
4
+ # This library is free software; you can redistribute it and/or
5
+ # modify it under the terms of the GNU Lesser General Public
6
+ # License as published by the Free Software Foundation; either
7
+ # version 2.1 of the License, or (at your option) any later version.
8
+
9
+ # This library is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ # Lesser General Public License for more details.
13
+
14
+ # You should have received a copy of the GNU Lesser General Public
15
+ # License along with this library; if not, write to the Free Software
16
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
+
18
+ module Requested
19
+ class Action < String
20
+ def rest?
21
+ %w(index new create edit update destroy show).include? self
22
+ end
23
+
24
+ def index?
25
+ self == "index"
26
+ end
27
+
28
+ def new?
29
+ self == "new"
30
+ end
31
+
32
+ def edit?
33
+ self == "edit"
34
+ end
35
+
36
+ def show?
37
+ self == "show"
38
+ end
39
+
40
+ def create?
41
+ self == "create"
42
+ end
43
+
44
+ def update?
45
+ self == "update"
46
+ end
47
+
48
+ def destroy?
49
+ self == "destroy"
50
+ end
51
+
52
+ def single_object?
53
+ self.rest? && !self.index
54
+ end
55
+
56
+ def collection?
57
+ self.index?
58
+ end
59
+
60
+ def new_or_create?
61
+ %w(new create).include? self
62
+ end
63
+
64
+ def edit_or_update?
65
+ %w(edit update).include? self
66
+ end
67
+ end
68
+ end
69
+
data/requested.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{requested}
5
+ s.version = "0.1.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Maurizio Casimirri"]
9
+ s.date = %q{2010-07-17}
10
+ s.description = %q{ActionController::Request improvements with several methods to get info on requested actions}
11
+ s.email = %q{maurizio.cas@gmail.com}
12
+ s.extra_rdoc_files = ["README.textile", "lib/requested.rb", "lib/requested/action.rb"]
13
+ s.files = ["README.textile", "Rakefile", "init.rb", "lib/requested.rb", "lib/requested/action.rb", "requested.gemspec", "Manifest"]
14
+ s.homepage = %q{http://github.com/mcasimir/requested}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Requested", "--main", "README.textile"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{requested}
18
+ s.rubygems_version = %q{1.3.7}
19
+ s.summary = %q{ActionController::Request improvements with several methods to get info on requested actions}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
+ else
27
+ end
28
+ else
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: requested
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - Maurizio Casimirri
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-17 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: ActionController::Request improvements with several methods to get info on requested actions
23
+ email: maurizio.cas@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.textile
30
+ - lib/requested.rb
31
+ - lib/requested/action.rb
32
+ files:
33
+ - README.textile
34
+ - Rakefile
35
+ - init.rb
36
+ - lib/requested.rb
37
+ - lib/requested/action.rb
38
+ - requested.gemspec
39
+ - Manifest
40
+ has_rdoc: true
41
+ homepage: http://github.com/mcasimir/requested
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --line-numbers
47
+ - --inline-source
48
+ - --title
49
+ - Requested
50
+ - --main
51
+ - README.textile
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 11
69
+ segments:
70
+ - 1
71
+ - 2
72
+ version: "1.2"
73
+ requirements: []
74
+
75
+ rubyforge_project: requested
76
+ rubygems_version: 1.3.7
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: ActionController::Request improvements with several methods to get info on requested actions
80
+ test_files: []
81
+