red_light 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ module RedLight
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../templates", __FILE__)
5
+
6
+ desc "Copy red light rules to your application."
7
+
8
+ def copy_rules
9
+ template "red_light_rules.rb", "config/initializers/red_light_rules.rb"
10
+ end
11
+
12
+ def show_readme
13
+ readme "README" if behavior == :invoke
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ ===============================================================================
2
+
3
+ Some setup you must do manually if you haven't yet:
4
+
5
+ ===============================================================================
@@ -0,0 +1,28 @@
1
+ # -*- encoding : utf-8 -*-
2
+ RED_LIGHT_RULES = Hash.new([])
3
+
4
+ controllers = Dir.glob("app/controllers/**/*_controller.rb").map do |entry|
5
+ entry.gsub(/\.rb$/, "").camelize.constantize
6
+ end
7
+
8
+ controllers.each do |c|
9
+ filter = c._process_action_callbacks.select{ |callback|
10
+ callback.filter == :authenticate_user!
11
+ }.first
12
+
13
+ next if filter.nil?
14
+
15
+ controller_name = c.to_s.underscore.split("_controller").first
16
+ RED_LIGHT_RULES[controller_name] = Array(c.action_methods)
17
+
18
+ options = filter.options
19
+ if options[:only].present? && Array(options[:only]).any?
20
+ RED_LIGHT_RULES[controller_name] = Array(options[:only]).map(&:to_s)
21
+ end
22
+
23
+ if options[:except].present? && Array(options[:except]).any?
24
+ RED_LIGHT_RULES[controller_name] -= Array(options[:except]).map(&:to_s)
25
+ end
26
+ end
27
+
28
+ RED_LIGHT_RULES.freeze
@@ -0,0 +1,62 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ module RedLight
4
+ class FancyLogin
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ dup._call(env)
11
+ end
12
+
13
+ def _call(env)
14
+ @status, @headers, @response = @app.call(env)
15
+
16
+ empty_response = (@response.is_a?(Array) && @response.size <= 1) ||
17
+ !@response.respond_to?(:body) ||
18
+ !@response.body.respond_to?(:empty?) ||
19
+ @response.body.empty?
20
+
21
+ return [@status, @headers, @response] if empty_response
22
+
23
+ response_body = []
24
+ @response.each do |part|
25
+ # For the forms
26
+ part.scan(/action="(.*?)"/).each do |form_action|
27
+ form_action = form_action.first
28
+ begin
29
+ path = Rails.application.routes.recognize_path(form_action, :method => "post")
30
+ controller = path[:controller]
31
+ action = path[:action]
32
+
33
+ if RED_LIGHT_RULES[controller] && RED_LIGHT_RULES[controller].include?(action)
34
+ part.gsub!(/action="#{form_action}"/, "action='javascript:void(0);' rel='fancy_login'")
35
+ end
36
+ rescue ActionController::RoutingError => e
37
+ end
38
+ end
39
+
40
+ # For the links
41
+ part.scan(/href="\/(.*?)"/).each do |href|
42
+ href = href.first
43
+ begin
44
+ path = Rails.application.routes.recognize_path(href)
45
+ controller = path[:controller]
46
+ action = path[:action]
47
+
48
+ if RED_LIGHT_RULES[controller] && RED_LIGHT_RULES[controller].include?(action)
49
+ href_regex = Regexp.compile(Regexp.escape('href="/'+href+'"'))
50
+ part.gsub!(href_regex, "href='/#{href}' rel='fancy_login'")
51
+ end
52
+ rescue ActionController::RoutingError => e
53
+ end
54
+ end
55
+
56
+ response_body << part
57
+ end
58
+
59
+ [@status, @headers, response_body]
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,9 @@
1
+ require 'red_light/fancy_login'
2
+
3
+ module RedLight
4
+ class Railtie < Rails::Railtie
5
+ initializer "red_light.configure_rails_initialization" do |app|
6
+ app.middleware.use RedLight::FancyLogin
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module RedLight
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/red_light.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "red_light/version"
2
+ require 'red_light/railtie' if defined?(Rails)
2
3
 
3
4
  module RedLight
4
- # Your code goes here...
5
5
  end
6
+
data/red_light.gemspec CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.version = RedLight::VERSION
9
9
  spec.authors = ["Hao Liu"]
10
10
  spec.email = ["leomayleomay@gmail.com"]
11
- spec.description = %q{a gem to add fancy login}
11
+ spec.description = %q{a gem used to add fancy login to your rails application}
12
12
  spec.summary = %q{fancy login it is}
13
13
  spec.homepage = "https://github.com/leomayleomay/red_light"
14
14
  spec.license = "MIT"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: red_light
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2013-05-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
- requirement: &70330299281640 !ruby/object:Gem::Requirement
16
+ requirement: &70216440716480 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '1.3'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70330299281640
24
+ version_requirements: *70216440716480
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70330299280220 !ruby/object:Gem::Requirement
27
+ requirement: &70216440712820 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,8 +32,8 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70330299280220
36
- description: a gem to add fancy login
35
+ version_requirements: *70216440712820
36
+ description: a gem used to add fancy login to your rails application
37
37
  email:
38
38
  - leomayleomay@gmail.com
39
39
  executables: []
@@ -46,7 +46,12 @@ files:
46
46
  - LICENSE.txt
47
47
  - README.md
48
48
  - Rakefile
49
+ - lib/generators/red_light/install_generator.rb
50
+ - lib/generators/templates/README
51
+ - lib/generators/templates/red_light_rules.rb
49
52
  - lib/red_light.rb
53
+ - lib/red_light/fancy_login.rb
54
+ - lib/red_light/railtie.rb
50
55
  - lib/red_light/version.rb
51
56
  - red_light.gemspec
52
57
  homepage: https://github.com/leomayleomay/red_light