padrino-contrib 0.0.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of padrino-contrib might be problematic. Click here for more details.

data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Padrino
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,29 @@
1
+ = Contributed Plugins and Utilities
2
+
3
+ This package includes a variety of add-on components for Padrino Framework:
4
+
5
+ * exception_notifier - Send errors through mail or/and to redmine
6
+ * orm_ar_permalink - Generate permalink for a specified column on ActiveRecord
7
+ * orm_ar_permalink_i18n - Generate permalink for a specified multi language column(s) on ActiveRecord
8
+ * orm_ar_translate - Translate for you your ActiveRecord columns
9
+ * auto_locale - Switch for you automatically the I18n.locale
10
+ * flash_session - Middleware that help you in passing your session in the URI, when it should be in the cookie.
11
+
12
+ === Use
13
+
14
+ In your Padrino project edit:
15
+
16
+ # Gemfile
17
+ gem 'padrino-contrib'
18
+
19
+ # boot.rb
20
+ require 'padrino-contrib/exception_notifier'
21
+ # require 'padrino-contrib/orm/ar/permalink'
22
+ # require 'padrino-contrib/orm/ar/textile'
23
+
24
+ Padrino.load! # Remember to add contribs before that line!
25
+
26
+ # app.rb
27
+ class MyApp < Padrino::Application
28
+ register Padrino::Contrib::ExceptionNotifier
29
+ end
data/Rakefile ADDED
@@ -0,0 +1,60 @@
1
+ require 'rubygems/specification' unless defined?(Gem::Specification)
2
+ require 'rake' unless defined?(Rake)
3
+
4
+ # Runs the sh command with sudo if the rake command is run with sudo
5
+ def sudo_sh(command)
6
+ command = `whoami`.strip! != "root" ? "sudo #{command}" : command
7
+ sh command
8
+ end
9
+
10
+ # Returns the gem specification object for a gem
11
+ def gemspec
12
+ @gemspec ||= begin
13
+ ::Gem::Specification.load("padrino-contrib.gemspec")
14
+ end
15
+ end
16
+
17
+ # Most notable functions are:
18
+ # $ rake package # packages the gem into the pkg folder
19
+ # $ rake install # installs the gem into system
20
+ # $ rake release # publishes gem to rubygems
21
+
22
+ desc "Validates the gemspec"
23
+ task :gemspec do
24
+ gemspec.validate
25
+ end
26
+
27
+ desc "Displays the current version"
28
+ task :version do
29
+ puts "Current version: #{gemspec.version}"
30
+ end
31
+
32
+ desc "Installs the gem locally"
33
+ task :install => :package do
34
+ sudo_sh "gem install pkg/#{gemspec.name}-#{gemspec.version}"
35
+ end
36
+
37
+ desc "Uninstalls the gem locally"
38
+ task :uninstall do
39
+ sudo_sh "gem uninstall padrino-contrib -v #{gemspec.version}"
40
+ end
41
+
42
+ desc "Release the gem"
43
+ task :release => :package do
44
+ sh "gem push pkg/#{gemspec.name}-#{gemspec.version}.gem"
45
+ end
46
+
47
+ # rake package
48
+ begin
49
+ require 'rake/gempackagetask'
50
+ rescue LoadError
51
+ task(:gem) { $stderr.puts '`gem install rake` to package gems' }
52
+ else
53
+ Rake::GemPackageTask.new(gemspec) do |pkg|
54
+ pkg.gem_spec = gemspec
55
+ end
56
+ task :gem => :gemspec
57
+ end
58
+
59
+ task :package => :gemspec
60
+ task :default => :install
@@ -0,0 +1,42 @@
1
+ module Padrino
2
+ module Contrib
3
+ ##
4
+ # This extension give to padrino the ability to change
5
+ # their locale inspecting.
6
+ #
7
+ # ==== Usage
8
+ #
9
+ # class MyApp < Padrino::Application
10
+ # register AutoLocale
11
+ # set :locales, [:en, :ru, :de] # First locale is the default locale
12
+ # end
13
+ #
14
+ # # view.haml
15
+ # =link_to "View this page in RU Version", switch_to_lang(:ru)
16
+ #
17
+ # So when we call an url like: /ru/blog/posts this extension set for you :ru as I18n.locale
18
+ #
19
+ module AutoLocale
20
+ module Helpers
21
+ ##
22
+ # This reload the page changing the I18n.locale
23
+ #
24
+ def switch_to_lang(lang)
25
+ request.path_info.sub(/\/#{I18n.locale}/, "/#{lang}") if options.locales.include?(lang)
26
+ end
27
+ end # Helpers
28
+
29
+ def self.registered(app)
30
+ app.helpers Padrino::Contrib::AutoLocale::Helpers
31
+ app.set :locales, [:en]
32
+ app.before do
33
+ if request.path_info =~ /^\/(#{options.locales.join('|')})\/?/
34
+ I18n.locale = $1.to_sym
35
+ else
36
+ I18n.locale = options.locales.first
37
+ end
38
+ end
39
+ end # self.registered
40
+ end # AutoLocale
41
+ end # Contrib
42
+ end # Padrino
@@ -0,0 +1,54 @@
1
+ ##
2
+ # This is an exception notifier for Padrino::Application with
3
+ # redmine bonus feature.
4
+ #
5
+ # ==== Usage
6
+ #
7
+ # class MyApp < Padrino::Application
8
+ # register Padrino::Contrib::ExceptionNotifier
9
+ # set :exceptions_from, "errors@localhost.local"
10
+ # set :exceptions_to, "foo@bar.local"
11
+ # set :exceptions_page, :errors # => views/errors.haml/erb
12
+ # set :redmine, :project => "My Bugs", :tracker => "Bugs", :priority => "High"
13
+ # # Uncomment this for test in development
14
+ # # disable :raise_errors
15
+ # # disable :show_exceptions
16
+ # end
17
+ #
18
+ module Padrino
19
+ module Contrib
20
+ module ExceptionNotifier
21
+
22
+ def self.registered(app)
23
+ app.set :exceptions_subject, "Exception"
24
+ app.set :exceptions_to, "errors@localhost.local"
25
+ app.set :exceptions_from, "foo@bar.local"
26
+ app.set :redmine, {}
27
+ app.error 500 do
28
+ boom = env['sinatra.error']
29
+ body = ["#{boom.class} - #{boom.message}:", *boom.backtrace].join("\n ")
30
+ body += "\n\n---Env:\n"
31
+ env.each { |k,v| body += "\n#{k}: #{v}" }
32
+ body += "\n\n---Params:\n"
33
+ params.each { |k,v| body += "\n#{k.inspect} => #{v.inspect}" }
34
+ logger.error body
35
+ settings.redmine.each { |k,v| body += "\n#{k.to_s.capitalize}: #{v}" }
36
+ app.email do
37
+ subject "[#{app.exceptions_subject}] #{boom.class} - #{boom.message}"
38
+ to app.exceptions_to
39
+ from app.exceptions_from
40
+ body body
41
+ end
42
+ response.status = 500
43
+ content_type 'text/html', :charset => "utf-8"
44
+ render settings.exceptions_page
45
+ end
46
+ app.error 404 do
47
+ response.status = 404
48
+ content_type 'text/html', :charset => "utf-8"
49
+ render settings.exceptions_page
50
+ end
51
+ end # self.registered
52
+ end # ExceptionNotifier
53
+ end # Contrib
54
+ end # Padrino
@@ -0,0 +1,32 @@
1
+ require 'rack/utils'
2
+
3
+ module Padrino
4
+ module Contrib
5
+ ##
6
+ # This Middleware help you passing your session in the URI, when it should be in the cookie.
7
+ #
8
+ # This code it's only performed when:
9
+ #
10
+ # env['HTTP_USER_AGENT'] =~ /^(Adobe|Shockwave) Flash/
11
+ #
12
+ # ==== Usage
13
+ #
14
+ # use FlashSessionMiddleware, session_id
15
+ #
16
+ class FlashSession
17
+ def initialize(app, session_key = 'session_id')
18
+ @app = app
19
+ @session_key = session_key.to_s
20
+ end
21
+
22
+ def call(env)
23
+ if env['HTTP_USER_AGENT'] =~ /^(Adobe|Shockwave) Flash/
24
+ params = ::Rack::Request.new(env).params
25
+ env['rack.session'] ||= {}
26
+ env['rack.session'][@session_key.to_sym] = params[@session_key] if params[@session_key].present?
27
+ end
28
+ @app.call(env)
29
+ end
30
+ end # FlashSession
31
+ end # Contrib
32
+ end # Padrino
@@ -0,0 +1,45 @@
1
+ module Padrino
2
+ module Contrib
3
+ module Orm
4
+ module Ar
5
+ ##
6
+ # This module extend ActiveRecord.
7
+ #
8
+ # You need to to your model a column called +:permalink+
9
+ #
10
+ # then use +has_permalink :title like:
11
+ #
12
+ # class Page < ActiveRecord::Base
13
+ # has_permalink :page
14
+ # end
15
+ #
16
+ module Permalink
17
+ module ClassMethods
18
+ def has_permalink(field)
19
+ include InstanceMethods
20
+ class_inheritable_accessor :permalink_field
21
+ write_inheritable_attribute :permalink_field, field
22
+ before_save :generate_permalink
23
+ end
24
+ end # ClassMethods
25
+
26
+ module InstanceMethods
27
+ def to_param
28
+ permalink
29
+ end
30
+
31
+ protected
32
+ def generate_permalink
33
+ self.permalink = read_attribute(permalink_field).downcase.
34
+ gsub(/\W/, '-').
35
+ gsub(/-+/, '-').
36
+ gsub(/-$/, '').
37
+ gsub(/^-/, '')
38
+ end
39
+ end # InstanceMethods
40
+ end # Permalink
41
+ end # Ar
42
+ end # Orm
43
+ end # Contrib
44
+ end # Padrino
45
+ ActiveRecord::Base.extend(Padrino::Contrib::Orm::Ar::Permalink::ClassMethods)
@@ -0,0 +1,56 @@
1
+ module Padrino
2
+ module Contrib
3
+ module Orm
4
+ module Ar
5
+ ##
6
+ # This module extend ActiveRecord.
7
+ #
8
+ # You need to to your model a column called +:permalink+
9
+ #
10
+ # then use +has_permalink :title like:
11
+ #
12
+ # class Page < ActiveRecord::Base
13
+ # has_permalink :page, :langs => [:en, :fr, :de]
14
+ # end
15
+ #
16
+ module PermalinkI18n
17
+ module ClassMethods
18
+ def has_permalink(field, options={})
19
+ include InstanceMethods
20
+ class_inheritable_accessor :permalink_field, :permalink_langs
21
+ write_inheritable_attribute :permalink_field, field
22
+ write_inheritable_attribute :permalink_langs, options.delete(:langs)
23
+ before_save :generate_permalinks
24
+ permalink_langs.each do |lang|
25
+ validates_uniqueness_of :"#{field}_#{lang}", options
26
+ end
27
+ end
28
+
29
+ def permalink_for(name)
30
+ name = Iconv.iconv('ascii//translit//IGNORE', 'utf-8', name).to_s
31
+ name.gsub!(/\W+/, ' ') # non-words to space
32
+ name.strip!
33
+ name.downcase!
34
+ name.gsub!(/\s+/, '-') # all spaces to dashes
35
+ name
36
+ end
37
+ end
38
+
39
+ module InstanceMethods
40
+ def to_param
41
+ permalink
42
+ end
43
+
44
+ protected
45
+ def generate_permalinks
46
+ permalink_langs.each do |lang|
47
+ self.send(:"permalink_#{lang}=", self.class.permalink_for(read_attribute(:"#{permalink_field}_#{lang}")))
48
+ end
49
+ end
50
+ end # InstanceMethods
51
+ end # PemalinkI18n
52
+ end # Ar
53
+ end # Orm
54
+ end # Contrib
55
+ end # Padrino
56
+ ActiveRecord::Base.extend(Padrino::Contrib::Orm::Ar::PermalinkI18n::ClassMethods)
@@ -0,0 +1,53 @@
1
+ require 'RedCloth'
2
+
3
+ module Padrino
4
+ module Contrib
5
+ module Orm
6
+ module Ar
7
+ ##
8
+ # This module generate html from textile.
9
+ #
10
+ # In your ActiveRecord class you need only to add:
11
+ #
12
+ # has_textile :body, :internal_links => :page
13
+ #
14
+ # In your body you can write (like github) internal links:
15
+ #
16
+ # [[Page Name|link me]]
17
+ #
18
+ module Textile
19
+ module ClassMethods
20
+ def has_textile(*fields)
21
+ include InstanceMethods
22
+ options = fields.extract_options!
23
+ options.reverse_merge!(:internal_links => :blog)
24
+ class_inheritable_accessor :textile_fields, :textile_options
25
+ write_inheritable_attribute :textile_fields, fields
26
+ write_inheritable_attribute :textile_options, options
27
+ before_save :generate_textile
28
+ end
29
+ end
30
+
31
+ module InstanceMethods
32
+ protected
33
+ def generate_textile
34
+ textile_fields.each do |textile_field|
35
+ next if read_attribute(textile_field).blank?
36
+ html = RedCloth.new(read_attribute(textile_field)).to_html
37
+ # Parse internal links
38
+ html.gsub!(/\[\[([^\]]+)\]\]/) do
39
+ page, name = *$1.split("|") # this allow to rename link ex: [[Page Name|link me]]
40
+ name ||= page
41
+ "<a href=\"/#{textile_options[:internal_links]}/#{Post.permalink_for(page.strip)}\">#{name.strip}</a>"
42
+ end
43
+ # Write content
44
+ self.send("#{textile_field}_html=", html)
45
+ end
46
+ end
47
+ end # InstanceMethods
48
+ end # Permalink
49
+ end # Ar
50
+ end # Orm
51
+ end # Contrib
52
+ end # Padrino
53
+ ActiveRecord::Base.extend(Padrino::Contrib::Orm::Ar::Textile::ClassMethods)
@@ -0,0 +1,43 @@
1
+ module Padrino
2
+ module Contrib
3
+ module Orm
4
+ module Ar
5
+ ##
6
+ # This is an extension for ActiveRecord where if I had:
7
+ #
8
+ # post.description_ru = "I'm Russian"
9
+ # post.description_en = "I'm English"
10
+ # post.description_it = "I'm Italian"
11
+ #
12
+ # with this extension if I had set:
13
+ #
14
+ # I18n.locale = :it
15
+ #
16
+ # calling directly:
17
+ #
18
+ # post.description
19
+ #
20
+ # will be a shortcut for:
21
+ #
22
+ # post.description_it => "I'm Italian"
23
+ #
24
+ module Translate
25
+ module ClassMethods
26
+ def has_locale
27
+ include InstanceMethods
28
+ end
29
+ end # ClassMethods
30
+
31
+ module InstanceMethods
32
+ def method_missing(method_name, *arguments)
33
+ attribute = "#{method_name}_#{I18n.locale}".to_sym
34
+ return self.send(attribute) if I18n.locale.present? && self.respond_to?(attribute)
35
+ super
36
+ end
37
+ end # InstanceMethods
38
+ end # ArTranslate
39
+ end # Ar
40
+ end # Orm
41
+ end # Contrib
42
+ end # Padrino
43
+ ActiveRecord::Base.extend(Padrino::Contrib::Orm::Ar::Translate::ClassMethods)
@@ -0,0 +1,17 @@
1
+ ##
2
+ # Manages current Padrino Contrib version for use in gem generation.
3
+ #
4
+ # We put this in a separate file so you can get padrino version
5
+ # without include full padrino contrib.
6
+ #
7
+ module Padrino
8
+ module Contrib
9
+ VERSION = '0.0.1' unless defined?(Padrino::Contrib::VERSION)
10
+ ##
11
+ # Return the current Padrino version
12
+ #
13
+ def self.version
14
+ VERSION
15
+ end
16
+ end # Contrib
17
+ end # Padrino
@@ -0,0 +1,18 @@
1
+ require File.expand_path("../lib/padrino-contrib/version.rb", __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "padrino-contrib"
5
+ s.rubyforge_project = "padrino-contrib"
6
+ s.authors = ["Padrino Team", "Nathan Esquenazi", "Davide D'Agostino", "Arthur Chiu"]
7
+ s.email = "padrinorb@gmail.com"
8
+ s.summary = "Contributed plugins and utilities for Padrino Framework"
9
+ s.homepage = "http://www.padrinorb.com"
10
+ s.description = "Contributed plugins and utilities for the Padrino Ruby Web Framework"
11
+ s.required_rubygems_version = ">= 1.3.6"
12
+ s.version = Padrino::Contrib.version
13
+ s.date = Time.now.strftime("%Y-%m-%d")
14
+ s.extra_rdoc_files = Dir["*.rdoc"]
15
+ s.files = %w(LICENSE README.rdoc Rakefile padrino-contrib.gemspec) + Dir.glob("{bin,lib,test}/**/*")
16
+ s.rdoc_options = ["--charset=UTF-8"]
17
+ s.require_path = "lib"
18
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: padrino-contrib
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Padrino Team
14
+ - Nathan Esquenazi
15
+ - Davide D'Agostino
16
+ - Arthur Chiu
17
+ autorequire:
18
+ bindir: bin
19
+ cert_chain: []
20
+
21
+ date: 2010-08-05 00:00:00 +02:00
22
+ default_executable:
23
+ dependencies: []
24
+
25
+ description: Contributed plugins and utilities for the Padrino Ruby Web Framework
26
+ email: padrinorb@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.rdoc
33
+ files:
34
+ - LICENSE
35
+ - README.rdoc
36
+ - Rakefile
37
+ - padrino-contrib.gemspec
38
+ - lib/padrino-contrib/auto_locale.rb
39
+ - lib/padrino-contrib/exception_notifier.rb
40
+ - lib/padrino-contrib/flash_session.rb
41
+ - lib/padrino-contrib/orm/ar/permalink.rb
42
+ - lib/padrino-contrib/orm/ar/permalink_i18n.rb
43
+ - lib/padrino-contrib/orm/ar/textile.rb
44
+ - lib/padrino-contrib/orm/ar/translate.rb
45
+ - lib/padrino-contrib/version.rb
46
+ has_rdoc: true
47
+ homepage: http://www.padrinorb.com
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --charset=UTF-8
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 23
70
+ segments:
71
+ - 1
72
+ - 3
73
+ - 6
74
+ version: 1.3.6
75
+ requirements: []
76
+
77
+ rubyforge_project: padrino-contrib
78
+ rubygems_version: 1.3.7
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: Contributed plugins and utilities for Padrino Framework
82
+ test_files: []
83
+