merb-to-rails3 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ pkg
2
+
@@ -0,0 +1,23 @@
1
+ # merb-to-rails3
2
+
3
+ ## About
4
+
5
+ **merb-to-rails3** is a gem which adds several methods from Merb's API to your Rails 3 app in order to ease updating existing Merb app to Rails.
6
+
7
+ Things covered:
8
+
9
+ Controller:
10
+
11
+ * redirect => redirect_to
12
+ * before => before_filter
13
+ * after => after_filter
14
+
15
+ Controller/View:
16
+
17
+ * url(name) => name_path
18
+ * resource(...) => ....._path
19
+ * submit => submit_tag
20
+ * css_include_tag => stylesheet_link_tag
21
+ * js_include_tag => javascript_include_tag
22
+ * throw_content(name) => content_for(name)
23
+ * partial => render :partial
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = 'merb-to-rails3'
9
+ gem.summary = 'Small library that will help you to port a Merb-based app to Rails3'
10
+ gem.email = "tech@llp.pl"
11
+ gem.homepage = "http://github.com/LunarLogicPolska/merb-to-rails3"
12
+ gem.authors = ['Marcin Kulik', 'Piotr Solnica']
13
+
14
+ gem.has_rdoc = false
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts 'Jeweler (or a dependency) not available. Install it with: sudo gem ' \
19
+ 'install jeweler'
20
+ end
@@ -0,0 +1,6 @@
1
+ * figure out why helper methods added during railtie initialization aren't visible later :)
2
+ * cover all controller methods
3
+ * cover all view helpers
4
+ * add spec matchers and various helper method proxies required in specs (ie "request" etc.)
5
+ * routes.rb generator (this should be fun!)
6
+ * rake task for renaming controllers (files + controller class names inside)
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 1
4
+ :patch: 0
@@ -0,0 +1,21 @@
1
+ dir = File.join(File.dirname(__FILE__), "merb-to-rails3")
2
+
3
+ require "#{dir}/helpers"
4
+ require "#{dir}/controller_exceptions"
5
+
6
+ module MerbToRails3
7
+ class Railtie < Rails::Railtie
8
+ railtie_name :"merb-to-rails3"
9
+
10
+ initializer "merb-to-rails3.rails" do
11
+ class ::ActionController::Base
12
+ extend MerbToRails3::Helpers::Controller
13
+ include MerbToRails3::ControllerExceptions
14
+ end
15
+
16
+ module ::ApplicationHelper
17
+ include MerbToRails3::Helpers::View
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,246 @@
1
+ class Exception
2
+ # Returns the action_name that will be invoked on your Exceptions controller when this
3
+ # exception is raised. Override this method to force a different action to be invoked.
4
+ #
5
+ # ==== Returns
6
+ # String:: The name of the action in the Exceptions controller which will get invoked
7
+ # when this exception is raised during a request.
8
+ #
9
+ # :api: public
10
+ # @overridable
11
+ def action_name() self.class.action_name end
12
+
13
+
14
+ # ==== Returns
15
+ # Boolean:: Whether or not this exception is the same as another.
16
+ #
17
+ # :api: public
18
+ def same?(other)
19
+ self.class == other.class &&
20
+ self.message == other.message &&
21
+ self.backtrace == other.backtrace
22
+ end
23
+
24
+ # Returns the action_name that will be invoked on your Exceptions controller when an instance
25
+ # is raised during a request.
26
+ #
27
+ # ==== Returns
28
+ # String:: The name of the action in the Exceptions controller which will get invoked
29
+ # when an instance of this Exception sub/class is raised by an action.
30
+ #
31
+ # :api: public
32
+ # @overridable
33
+ def self.action_name
34
+ name = self.to_s.split('::').last.underscore
35
+ ApplicationController.method_defined?(name) ? name : superclass.action_name
36
+ end
37
+
38
+ def self.status
39
+ 500
40
+ end
41
+ end
42
+
43
+ module MerbToRails3
44
+ module ControllerExceptions
45
+
46
+ # Mapping of status code names to their numeric value.
47
+ STATUS_CODES = {}
48
+
49
+ class Base < StandardError #:doc:
50
+
51
+ # === Returns
52
+ # Integer:: The status-code of the error.
53
+ #
54
+ # @overridable
55
+ # :api: plugin
56
+ def status; self.class.status; end
57
+ alias :to_i :status
58
+
59
+ class << self
60
+
61
+ # Get the actual status-code for an Exception class.
62
+ #
63
+ # As usual, this can come from a constant upwards in
64
+ # the inheritance chain.
65
+ #
66
+ # ==== Returns
67
+ # Fixnum:: The status code of this exception.
68
+ #
69
+ # :api: public
70
+ def status
71
+ const_get(:STATUS) rescue 0
72
+ end
73
+ alias :to_i :status
74
+
75
+ # Set the actual status-code for an Exception class.
76
+ #
77
+ # If possible, set the STATUS constant, and update
78
+ # any previously registered (inherited) status-code.
79
+ #
80
+ # ==== Parameters
81
+ # num<~to_i>:: The status code
82
+ #
83
+ # ==== Returns
84
+ # (Integer, nil):: The status set on this exception, or nil if a status was already set.
85
+ #
86
+ # :api: private
87
+ def status=(num)
88
+ unless self.status?
89
+ register_status_code(self, num)
90
+ self.const_set(:STATUS, num.to_i)
91
+ end
92
+ end
93
+
94
+ # See if a status-code has been defined (on self explicitly).
95
+ #
96
+ # ==== Returns
97
+ # Boolean:: Whether a status code has been set
98
+ #
99
+ # :api: private
100
+ def status?
101
+ self.const_defined?(:STATUS)
102
+ end
103
+
104
+ # Registers any subclasses with status codes for easy lookup by
105
+ # set_status in Merb::Controller.
106
+ #
107
+ # Inheritance ensures this method gets inherited by any subclasses, so
108
+ # it goes all the way down the chain of inheritance.
109
+ #
110
+ # ==== Parameters
111
+ #
112
+ # subclass<ControllerExceptions::Base>::
113
+ # The Exception class that is inheriting from ControllerExceptions::Base
114
+ #
115
+ # :api: public
116
+ def inherited(subclass)
117
+ # don't set the constant yet - any class methods will be called after self.inherited
118
+ # unless self.status = ... is set explicitly, the status code will be inherited
119
+ register_status_code(subclass, self.status) if self.status?
120
+ end
121
+
122
+ private
123
+
124
+ # Register the status-code for an Exception class.
125
+ #
126
+ # ==== Parameters
127
+ # num<~to_i>:: The status code
128
+ #
129
+ # :api: privaate
130
+ def register_status_code(klass, code)
131
+ name = self.to_s.split('::').last.underscore
132
+ STATUS_CODES[name.to_sym] = code.to_i
133
+ end
134
+
135
+ end
136
+ end
137
+
138
+ class Informational < ControllerExceptions::Base; end
139
+
140
+ class Continue < ControllerExceptions::Informational; self.status = 100; end
141
+
142
+ class SwitchingProtocols < ControllerExceptions::Informational; self.status = 101; end
143
+
144
+ class Successful < ControllerExceptions::Base; end
145
+
146
+ class OK < ControllerExceptions::Successful; self.status = 200; end
147
+
148
+ class Created < ControllerExceptions::Successful; self.status = 201; end
149
+
150
+ class Accepted < ControllerExceptions::Successful; self.status = 202; end
151
+
152
+ class NonAuthoritativeInformation < ControllerExceptions::Successful; self.status = 203; end
153
+
154
+ class NoContent < ControllerExceptions::Successful; self.status = 204; end
155
+
156
+ class ResetContent < ControllerExceptions::Successful; self.status = 205; end
157
+
158
+ class PartialContent < ControllerExceptions::Successful; self.status = 206; end
159
+
160
+ class Redirection < ControllerExceptions::Base; end
161
+
162
+ class MultipleChoices < ControllerExceptions::Redirection; self.status = 300; end
163
+
164
+ class MovedPermanently < ControllerExceptions::Redirection; self.status = 301; end
165
+
166
+ class MovedTemporarily < ControllerExceptions::Redirection; self.status = 302; end
167
+
168
+ class SeeOther < ControllerExceptions::Redirection; self.status = 303; end
169
+
170
+ class NotModified < ControllerExceptions::Redirection; self.status = 304; end
171
+
172
+ class UseProxy < ControllerExceptions::Redirection; self.status = 305; end
173
+
174
+ class TemporaryRedirect < ControllerExceptions::Redirection; self.status = 307; end
175
+
176
+ class ClientError < ControllerExceptions::Base; end
177
+
178
+ class BadRequest < ControllerExceptions::ClientError; self.status = 400; end
179
+
180
+ class MultiPartParseError < ControllerExceptions::BadRequest; end
181
+
182
+ class Unauthorized < ControllerExceptions::ClientError; self.status = 401; end
183
+
184
+ class PaymentRequired < ControllerExceptions::ClientError; self.status = 402; end
185
+
186
+ class Forbidden < ControllerExceptions::ClientError; self.status = 403; end
187
+
188
+ class NotFound < ControllerExceptions::ClientError; self.status = 404; end
189
+
190
+ class ActionNotFound < ControllerExceptions::NotFound; end
191
+
192
+ class TemplateNotFound < ControllerExceptions::NotFound; end
193
+
194
+ class LayoutNotFound < ControllerExceptions::NotFound; end
195
+
196
+ class MethodNotAllowed < ControllerExceptions::ClientError; self.status = 405; end
197
+
198
+ class NotAcceptable < ControllerExceptions::ClientError; self.status = 406; end
199
+
200
+ class ProxyAuthenticationRequired < ControllerExceptions::ClientError; self.status = 407; end
201
+
202
+ class RequestTimeout < ControllerExceptions::ClientError; self.status = 408; end
203
+
204
+ class Conflict < ControllerExceptions::ClientError; self.status = 409; end
205
+
206
+ class Gone < ControllerExceptions::ClientError; self.status = 410; end
207
+
208
+ class LengthRequired < ControllerExceptions::ClientError; self.status = 411; end
209
+
210
+ class PreconditionFailed < ControllerExceptions::ClientError; self.status = 412; end
211
+
212
+ class RequestEntityTooLarge < ControllerExceptions::ClientError; self.status = 413; end
213
+
214
+ class RequestURITooLarge < ControllerExceptions::ClientError; self.status = 414; end
215
+
216
+ class UnsupportedMediaType < ControllerExceptions::ClientError; self.status = 415; end
217
+
218
+ class RequestRangeNotSatisfiable < ControllerExceptions::ClientError; self.status = 416; end
219
+
220
+ class ExpectationFailed < ControllerExceptions::ClientError; self.status = 417; end
221
+
222
+ class ServerError < ControllerExceptions::Base; end
223
+
224
+ class InternalServerError < ControllerExceptions::ServerError; self.status = 500; end
225
+
226
+ class NotImplemented < ControllerExceptions::ServerError; self.status = 501; end
227
+
228
+ class BadGateway < ControllerExceptions::ServerError; self.status = 502; end
229
+
230
+ class ServiceUnavailable < ControllerExceptions::ServerError; self.status = 503; end
231
+
232
+ class GatewayTimeout < ControllerExceptions::ServerError; self.status = 504; end
233
+
234
+ class HTTPVersionNotSupported < ControllerExceptions::ServerError; self.status = 505; end
235
+ end
236
+
237
+ # Required to show exceptions in the log file
238
+ #
239
+ # e<Exception>:: The exception that a message is being generated for
240
+ #
241
+ # :api: plugin
242
+ def self.exception(e)
243
+ "#{ e.message } - (#{ e.class })\n" <<
244
+ "#{(e.backtrace or []).join("\n")}"
245
+ end
246
+ end
@@ -0,0 +1,110 @@
1
+ def merb_deprec(notice=nil)
2
+ msg = "!!! #{caller.first[/in `(.+)'$/, 1]} IS DEPRECATED (found at #{self}:#{caller.first[/:(\d+):in/, 1]})"
3
+ if notice
4
+ msg << ": #{notice}"
5
+ end
6
+ Rails.logger.warn(msg)
7
+ end
8
+
9
+ module MerbToRails3
10
+ module Helpers
11
+ module ViewAndController
12
+ def url(name)
13
+ path = "#{name}_path"
14
+ merb_deprec("use #{path}")
15
+ send(path)
16
+ end
17
+
18
+ def resource(*args)
19
+ opts_hash = args.last.is_a?(Hash) ? args.pop : {}
20
+ action = args.pop if args.size > 1 && [:new, :edit, :delete].include?(args.last)
21
+ objects = []
22
+ resources = args.map do |a|
23
+ if a.is_a?(String) || a.is_a?(Symbol)
24
+ a.to_s.underscore
25
+ else
26
+ objects << a
27
+ a.class.to_s.underscore
28
+ end
29
+ end
30
+ if action
31
+ resources.unshift(action)
32
+ resources << resources.pop.to_s.singularize
33
+ end
34
+ path = "#{resources.join('_')}_path"
35
+ merb_deprec("use #{path}")
36
+ objects.empty? ? send(path, opts_hash) : send(path, *(objects << opts_hash))
37
+ end
38
+
39
+ def partial(name, opts={})
40
+ merb_deprec("use render :partial")
41
+ args = { :partial => name.to_s, :locals => opts }
42
+ if opts.key?(:with)
43
+ args[:object] = opts.delete(:with)
44
+ end
45
+ render args
46
+ end
47
+
48
+ def message
49
+ merb_deprec("use flash")
50
+ flash
51
+ end
52
+ end
53
+
54
+ module View
55
+ include ViewAndController
56
+
57
+ def submit(value, options)
58
+ merb_deprec("use submit_tag")
59
+ submit_tag(value, options)
60
+ end
61
+
62
+ def css_include_tag(*args)
63
+ merb_deprec("use stylesheet_link_tag")
64
+ stylesheet_link_tag(*args)
65
+ end
66
+
67
+ def js_include_tag(*args)
68
+ merb_deprec("use javascript_include_tag")
69
+ javascript_include_tag(*args)
70
+ end
71
+
72
+ def throw_content(name, *args, &blk)
73
+ content_for(name, *args, &blk)
74
+ end
75
+ end
76
+
77
+ module Controller
78
+ def self.extended(base)
79
+ base.send(:include, InstanceMethods)
80
+ end
81
+
82
+ def before(*args)
83
+ merb_deprec("use before_filter")
84
+ before_filter(*args)
85
+ end
86
+
87
+ def after(*args)
88
+ merb_deprec("use after_filter")
89
+ after_filter(*args)
90
+ end
91
+
92
+ module InstanceMethods
93
+ include ViewAndController
94
+
95
+ def redirect(*args)
96
+ opts = args.dup.extract_options!
97
+
98
+ if message = opts.delete(:message)
99
+ message.each { |type, text| flash[type] = text }
100
+ merb_deprec("use flash instead of :message option in redirect")
101
+ end
102
+
103
+ merb_deprec("use redirect_to")
104
+
105
+ redirect_to(*args)
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,45 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{merb-to-rails3}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Marcin Kulik", "Piotr Solnica"]
12
+ s.date = %q{2010-03-31}
13
+ s.email = %q{tech@llp.pl}
14
+ s.extra_rdoc_files = [
15
+ "README.markdown"
16
+ ]
17
+ s.files = [
18
+ ".gitignore",
19
+ "README.markdown",
20
+ "Rakefile",
21
+ "TODO.markdown",
22
+ "VERSION.yml",
23
+ "lib/merb-to-rails3.rb",
24
+ "lib/merb-to-rails3/controller_exceptions.rb",
25
+ "lib/merb-to-rails3/helpers.rb",
26
+ "merb-to-rails3.gemspec"
27
+ ]
28
+ s.has_rdoc = false
29
+ s.homepage = %q{http://github.com/LunarLogicPolska/merb-to-rails3}
30
+ s.rdoc_options = ["--charset=UTF-8"]
31
+ s.require_paths = ["lib"]
32
+ s.rubygems_version = %q{1.3.6}
33
+ s.summary = %q{Small library that will help you to port a Merb-based app to Rails3}
34
+
35
+ if s.respond_to? :specification_version then
36
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
37
+ s.specification_version = 3
38
+
39
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
40
+ else
41
+ end
42
+ else
43
+ end
44
+ end
45
+
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: merb-to-rails3
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Marcin Kulik
13
+ - Piotr Solnica
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-03-31 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description:
23
+ email: tech@llp.pl
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.markdown
30
+ files:
31
+ - .gitignore
32
+ - README.markdown
33
+ - Rakefile
34
+ - TODO.markdown
35
+ - VERSION.yml
36
+ - lib/merb-to-rails3.rb
37
+ - lib/merb-to-rails3/controller_exceptions.rb
38
+ - lib/merb-to-rails3/helpers.rb
39
+ - merb-to-rails3.gemspec
40
+ has_rdoc: false
41
+ homepage: http://github.com/LunarLogicPolska/merb-to-rails3
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --charset=UTF-8
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.6
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Small library that will help you to port a Merb-based app to Rails3
70
+ test_files: []
71
+