hancock_cms_goto 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.ruby-gemset +1 -0
  4. data/.ruby-version +1 -0
  5. data/.travis.yml +4 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +56 -0
  9. data/Rakefile +1 -0
  10. data/app/controllers/concerns/hancock/goto/decorators/transfers.rb +29 -0
  11. data/app/controllers/hancock/goto/transfers_controller.rb +7 -0
  12. data/app/models/concerns/hancock/goto/decorators/transfer.rb +53 -0
  13. data/app/models/hancock/goto/transfer.rb +11 -0
  14. data/bin/console +14 -0
  15. data/bin/setup +7 -0
  16. data/config/locales/hancock.goto.ru.yml +17 -0
  17. data/hancock_cms_goto.gemspec +38 -0
  18. data/lib/generators/hancock/goto/config/config_generator.rb +13 -0
  19. data/lib/generators/hancock/goto/config/templates/hancock_goto.erb +16 -0
  20. data/lib/generators/hancock/goto/controllers/decorators_generator.rb +24 -0
  21. data/lib/generators/hancock/goto/controllers/templates/transfers.erb +25 -0
  22. data/lib/generators/hancock/goto/controllers/transfers_generator.rb +39 -0
  23. data/lib/generators/hancock/goto/models/decorators_generator.rb +24 -0
  24. data/lib/generators/hancock/goto/models/templates/transfer.erb +46 -0
  25. data/lib/generators/hancock/goto/models/transfer_generator.rb +39 -0
  26. data/lib/hancock/goto/admin.rb +6 -0
  27. data/lib/hancock/goto/admin/transfer.rb +74 -0
  28. data/lib/hancock/goto/configuration.rb +46 -0
  29. data/lib/hancock/goto/controllers/transfers.rb +30 -0
  30. data/lib/hancock/goto/engine.rb +7 -0
  31. data/lib/hancock/goto/middleware.rb +125 -0
  32. data/lib/hancock/goto/models/mongoid/transfer.rb +39 -0
  33. data/lib/hancock/goto/models/transfer.rb +39 -0
  34. data/lib/hancock/goto/routes.rb +15 -0
  35. data/lib/hancock/goto/version.rb +5 -0
  36. data/lib/hancock_cms_goto.rb +34 -0
  37. data/release.sh +6 -0
  38. metadata +156 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 521e78d560623c7abee4d147559faa673c55c72f
4
+ data.tar.gz: 3ee68ad179c6a17aa5b5d63cfe87a694ee4d9af9
5
+ SHA512:
6
+ metadata.gz: e87a3d30d5a0b2b86b7a863da245bf99afec77c1ec19d8e3151fc1341d77e45159c1a990185216f3009037036d1e71d026c1a8e0dec33df763e41a9954a1344d
7
+ data.tar.gz: f6a8b1138576e15258bb1692c6425642d4320f998c81af5c10817e0fbe4e5affe56e8b328ff6f9f3f9b539528fbf1ac5bca230f47bc2ca7d08fbf5c2d35fdf21
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ hancock_cms_goto
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.1
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hancock_cms_goto.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Alexander Kiseliev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # HancockCmsGoto
2
+
3
+ ### Remaded from [EnjoyCMSGoto](https://github.com/enjoycreative/enjoy_cms_goto)
4
+
5
+ URL redirect dispatcher (goto-system) for [HancockCMS](https://github.com/red-rocks/hancock_cms). You will know all about transfers to other sites.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'hancock_cms_goto'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install hancock_cms_goto
22
+
23
+ ## Usage
24
+
25
+ Add in config/routes.rb
26
+
27
+ ```ruby
28
+ hancock_cms_goto_routes
29
+ ```
30
+
31
+ and in config/application.rb
32
+
33
+ ```ruby
34
+ config.middleware.use Hancock::Goto::Middleware
35
+ ```
36
+
37
+ Then execute
38
+
39
+ $ rails g hancock:goto:config
40
+
41
+ and now you can edit config/initializers/hancock_goto.rb
42
+
43
+ ## Development
44
+
45
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
46
+
47
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/red-rocks/hancock_cms_goto.
52
+
53
+
54
+ ## License
55
+
56
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,29 @@
1
+ module Hancock::Goto::Decorators
2
+ module Transfers
3
+ extend ActiveSupport::Concern
4
+
5
+ # def index
6
+ # url = Addressable::URI.heuristic_parse(params[:url]) rescue nil
7
+ # referer = (request.referer ? Addressable::URI.parse(request.referer) : nil) rescue nil
8
+ #
9
+ # @transfer = transfer_class.new
10
+ # @transfer.recieved_url = params[:url]
11
+ # @transfer.url = url.to_s
12
+ # @transfer.host = url.host.to_s if url
13
+ # @transfer.referer = referer.to_s
14
+ # @transfer.source_ip = request.env['HTTP_X_FORWARDED_FOR'] || request.remote_ip
15
+ # if Hancock::Goto.mongoid?
16
+ # @transfer.session_id = session.id
17
+ # @transfer.session_data = BSON::Binary.new(Marshal.dump(session.to_hash))
18
+ # end
19
+ # @transfer.save
20
+ #
21
+ # redirect_to @transfer.url, code: 303
22
+ # end
23
+ #
24
+ # def transfer_class
25
+ # Hancock::Goto::Transfer
26
+ # end
27
+
28
+ end
29
+ end
@@ -0,0 +1,7 @@
1
+ module Hancock::Goto
2
+ class TransfersController < ApplicationController
3
+ include Hancock::Goto::Controllers::Transfers
4
+
5
+ include Hancock::Goto::Decorators::Transfers
6
+ end
7
+ end
@@ -0,0 +1,53 @@
1
+ module Hancock::Goto::Decorators
2
+ module Transfer
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ # if Hancock.rails4?
7
+ # belongs_to :session, class_name: "MongoidStore::Session", overwrite: true
8
+ # else
9
+ # belongs_to :session, class_name: "MongoidStore::Session", overwrite: true, optional: true
10
+ # end
11
+ # field :session_data, type: BSON::Binary, overwrite: true
12
+
13
+ # def session_data_extract
14
+ # session_data.data if session_data
15
+ # end
16
+ #
17
+ # def session_data_unpack
18
+ # Marshal.load(session_data_extract) if session_data
19
+ # end
20
+ #
21
+ # def set_session(_session)
22
+ # self.session_id = _session.id
23
+ # self.session_data = BSON::Binary.new(Marshal.dump(_session.to_hash))
24
+ # end
25
+
26
+ ############# rails_admin ##############
27
+ # def self.rails_admin_add_fields
28
+ # [] #super
29
+ # end
30
+ #
31
+ # def self.rails_admin_add_config(config)
32
+ # #super(config)
33
+ # end
34
+ #
35
+ # def self.admin_can_user_defined_actions
36
+ # [].freeze
37
+ # end
38
+ # def self.admin_cannot_user_defined_actions
39
+ # [].freeze
40
+ # end
41
+ # def self.manager_can_user_defined_actions
42
+ # [].freeze
43
+ # end
44
+ # def self.manager_cannot_user_defined_actions
45
+ # [].freeze
46
+ # end
47
+ # def self.rails_admin_user_defined_visible_actions
48
+ # [].freeze
49
+ # end
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,11 @@
1
+ module Hancock::Goto
2
+ class Transfer
3
+ include Hancock::Goto::Models::Transfer
4
+
5
+ include Hancock::Goto::Decorators::Transfer
6
+
7
+ rails_admin(&Hancock::Goto::Admin::Transfer.config(rails_admin_add_fields) { |config|
8
+ rails_admin_add_config(config)
9
+ })
10
+ end
11
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hancock_cms_goto"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,17 @@
1
+ ru:
2
+ hancock:
3
+ goto: 'Переходы с сайта'
4
+ mongoid: &mongoid
5
+ models:
6
+ hancock/goto/transfer: "Переходы с сайта"
7
+ attributes:
8
+ hancock/goto/transfer:
9
+ url: URL перехода
10
+ recieved_url: Принятый URL
11
+ host: Хост перехода
12
+ referer: Страница, с которой перешли
13
+ source_ip: IP пользователя
14
+ creator: Пользователь
15
+
16
+ activerecord:
17
+ <<: *mongoid
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hancock/goto/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hancock_cms_goto"
8
+ spec.version = Hancock::Goto::VERSION
9
+ spec.authors = ["Alexander Kiseliev"]
10
+ spec.email = ["dev@redrocks.pro"]
11
+
12
+ spec.summary = %q{URL redirect dispatcher with HancockCMS support.}
13
+ spec.description = %q{URL redirect dispatcher with HancockCMS support.}
14
+ spec.homepage = "https://github.com/red-rocks/hancock_cms_goto"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.10"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+
33
+ spec.add_dependency 'hancock_cms', [">=1.0", "<=2.1"]
34
+ # spec.add_dependency 'hancock_cms', ["~> 1.0", "~> 2.1"]
35
+
36
+ spec.add_dependency 'nokogiri'
37
+ spec.add_dependency 'addressable'
38
+ end
@@ -0,0 +1,13 @@
1
+ require 'rails/generators'
2
+
3
+ module Hancock::Goto
4
+ class ConfigGenerator < Rails::Generators::Base
5
+ source_root File.expand_path('../templates', __FILE__)
6
+
7
+ desc 'Hancock::Goto Config generator'
8
+ def config
9
+ template 'hancock_goto.erb', "config/initializers/hancock_goto.rb"
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ Hancock::Goto.configure do |config|
2
+ ##### defaults #####
3
+ # config.css_selector = "a[href]"
4
+ # config.href_regex = /^(https?:)?\/\//i
5
+ # config.excluded_hosts = []
6
+
7
+ # config.add_nofollow = true
8
+ # config.add_noindex = true
9
+ # config.add_noreferrer = true
10
+ # config.add_noopener = true
11
+ # config.del_attrs = true
12
+
13
+ # config.model_settings_support = defined?(RailsAdminModelSettings)
14
+ # config.user_abilities_support = defined?(RailsAdminUserAbilities)
15
+ # config.ra_comments_support = defined?(RailsAdminComments)
16
+ end
@@ -0,0 +1,24 @@
1
+ require 'rails/generators'
2
+
3
+ module Hancock::Goto::Controllers
4
+ class DecoratorsGenerator < Rails::Generators::Base
5
+ source_root File.expand_path('../../../../../../app/controllers/concerns/hancock/goto/decorators', __FILE__)
6
+ argument :controllers, type: :array, default: []
7
+
8
+ desc 'Hancock::Goto Controllers decorators generator'
9
+ def decorators
10
+ copied = false
11
+ (controllers == ['all'] ? permitted_controllers : controllers & permitted_controllers).each do |m|
12
+ copied = true
13
+ copy_file "#{m}.rb", "app/controllers/concerns/hancock/goto/decorators/#{m}.rb"
14
+ end
15
+ puts "U need to set controllers`s name. One of this: #{permitted_controllers.join(", ")}." unless copied
16
+ end
17
+
18
+ private
19
+ def permitted_controllers
20
+ ['transfers']
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ class <%= camelcased_class_name %> < Hancock::Goto::TransfersController
2
+
3
+ # def index
4
+ # url = Addressable::URI.heuristic_parse(params[:url]) rescue nil
5
+ # referer = (request.referer ? Addressable::URI.parse(request.referer) : nil) rescue nil
6
+ #
7
+ # @transfer = transfer_class.new
8
+ # @transfer.recieved_url = params[:url]
9
+ # @transfer.url = url.to_s
10
+ # @transfer.host = url.host.to_s if url
11
+ # @transfer.referer = referer.to_s
12
+ # @transfer.source_ip = request.env['HTTP_X_FORWARDED_FOR'] || request.remote_ip
13
+ # if Hancock::Goto.mongoid?
14
+ # @transfer.set_session(session)
15
+ # end
16
+ # @transfer.save
17
+ #
18
+ # redirect_to @transfer.url, code: 303
19
+ # end
20
+ #
21
+ # def transfer_class
22
+ # Hancock::Goto::Transfer
23
+ # end
24
+
25
+ end
@@ -0,0 +1,39 @@
1
+ require 'rails/generators'
2
+
3
+ module Hancock::Goto::Controllers
4
+ class TransfersGenerator < Rails::Generators::Base
5
+ source_root File.expand_path('../templates', __FILE__)
6
+ argument :class_name_arg, type: :string, default: ""
7
+
8
+ desc 'Hancock::Goto Transfer Controller generator'
9
+ def transfers
10
+ template 'transfers.erb', "app/controllers/#{file_name}.rb"
11
+ end
12
+
13
+ private
14
+ def class_name
15
+ class_name_arg.blank? ? "SetClassForTransferController" : class_name_arg
16
+ end
17
+
18
+ def capitalized_class_name
19
+ class_name.capitalize
20
+ end
21
+
22
+ def camelcased_class_name
23
+ class_name.camelcase
24
+ end
25
+
26
+ def file_name
27
+ underscored_class_name
28
+ end
29
+
30
+ def underscored_class_name
31
+ camelcased_class_name.underscore
32
+ end
33
+
34
+ def underscored_pluralized_class_name
35
+ underscored_class_name.pluralize
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,24 @@
1
+ require 'rails/generators'
2
+
3
+ module Hancock::Goto::Models
4
+ class DecoratorsGenerator < Rails::Generators::Base
5
+ source_root File.expand_path('../../../../../../app/models/concerns/hancock/goto/decorators', __FILE__)
6
+ argument :models, type: :array, default: []
7
+
8
+ desc 'Hancock::Goto Models decorators generator'
9
+ def decorators
10
+ copied = false
11
+ (models == ['all'] ? permitted_models : models & permitted_models).each do |c|
12
+ copied = true
13
+ copy_file "#{c}.rb", "app/models/concerns/hancock/goto/decorators/#{c}.rb"
14
+ end
15
+ puts "U need to set controller`s name. One of this: #{permitted_models.join(", ")}." unless copied
16
+ end
17
+
18
+ private
19
+ def permitted_models
20
+ ['transfer']
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,46 @@
1
+ class <%= camelcased_class_name %> < Hancock::Goto::Transfer
2
+
3
+ # if Hancock.rails4?
4
+ # belongs_to :session, class_name: "MongoidStore::Session", overwrite: true
5
+ # else
6
+ # belongs_to :session, class_name: "MongoidStore::Session", overwrite: true, optional: true
7
+ # end
8
+ # field :session_data, type: BSON::Binary, overwrite: true
9
+
10
+ # def session_data_extract
11
+ # session_data.data if session_data
12
+ # end
13
+ #
14
+ # def session_data_unpack
15
+ # Marshal.load(session_data_extract) if session_data
16
+ # end
17
+
18
+ ############ rails_admin ##############
19
+ def self.rails_admin_add_fields
20
+ [] #super
21
+ end
22
+
23
+ def self.rails_admin_add_config(config)
24
+ #super(config)
25
+ end
26
+
27
+ def admin_can_user_defined_actions
28
+ [].freeze
29
+ end
30
+ def admin_cannot_user_defined_actions
31
+ [].freeze
32
+ end
33
+ def manager_can_user_defined_actions
34
+ [].freeze
35
+ end
36
+ def manager_cannot_user_defined_actions
37
+ [].freeze
38
+ end
39
+ def rails_admin_user_defined_visible_actions
40
+ [].freeze
41
+ end
42
+
43
+ rails_admin(&Hancock::Goto::Admin::Transfer.config(rails_admin_add_fields) { |config|
44
+ rails_admin_add_config(config)
45
+ })
46
+ end
@@ -0,0 +1,39 @@
1
+ require 'rails/generators'
2
+
3
+ module Hancock::Goto::Models
4
+ class TransferGenerator < Rails::Generators::Base
5
+ source_root File.expand_path('../templates', __FILE__)
6
+ argument :class_name_arg, type: :string, default: ""
7
+
8
+ desc 'Hancock::Goto Transfer Model generator'
9
+ def transfer
10
+ template 'transfer.erb', "app/models/#{file_name}.rb"
11
+ end
12
+
13
+ private
14
+ def class_name
15
+ class_name_arg.blank? ? "SetClassForTransfer" : class_name_arg
16
+ end
17
+
18
+ def capitalized_class_name
19
+ class_name.capitalize
20
+ end
21
+
22
+ def camelcased_class_name
23
+ class_name.camelcase
24
+ end
25
+
26
+ def file_name
27
+ underscored_class_name
28
+ end
29
+
30
+ def underscored_class_name
31
+ camelcased_class_name.underscore
32
+ end
33
+
34
+ def underscored_pluralized_class_name
35
+ underscored_class_name.pluralize
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,6 @@
1
+ module Hancock::Goto
2
+ module Admin
3
+ class << self
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,74 @@
1
+ module Hancock::Goto
2
+ module Admin
3
+ module Transfer
4
+ def self.config(fields = {})
5
+ Proc.new {
6
+ if defined?(Hancock::Feedback)
7
+ navigation_label I18n.t('hancock.feedback')
8
+ else
9
+ navigation_label I18n.t('hancock.goto')
10
+ end
11
+
12
+ field :creator do
13
+ filterable true
14
+ pretty_value do
15
+ unless bindings[:object].creator.blank?
16
+ route = (bindings[:view] || bindings[:controller])
17
+ creator = bindings[:object].creator
18
+ model_name = creator.class.to_param.gsub("::", "~").underscore
19
+ _url = route.rails_admin.show_path(model_name: model_name, id: creator._id)
20
+ route.link_to(creator.name_for_hancock_cms, _url, class: "creator_link")
21
+ end
22
+ end
23
+ end
24
+ field :created_at
25
+ field :url do
26
+ searchable true
27
+ pretty_value do
28
+ unless bindings[:object].url.blank?
29
+ route = (bindings[:view] || bindings[:controller])
30
+ _target = Addressable::URI.parse(bindings[:object].url).to_s
31
+ route.link_to(_target, _target, title: _target, target: :_blank)
32
+ end
33
+ end
34
+ end
35
+ field :recieved_url
36
+ field :host do
37
+ searchable true
38
+ pretty_value do
39
+ unless bindings[:object].host.blank?
40
+ route = (bindings[:view] || bindings[:controller])
41
+ _target = Addressable::URI.parse(bindings[:object].host).to_s
42
+ route.link_to(_target, _target, title: _target, target: :_blank)
43
+ end
44
+ end
45
+ end
46
+ field :referer do
47
+ searchable true
48
+ pretty_value do
49
+ unless bindings[:object].referer.blank?
50
+ route = (bindings[:view] || bindings[:controller])
51
+ _target = Addressable::URI.parse(bindings[:object].referer).to_s
52
+ route.link_to(_target, _target, title: _target, target: :_blank)
53
+ end
54
+ end
55
+ end
56
+ field :source_ip do
57
+ searchable true
58
+ end
59
+ field :session_id do
60
+ searchable true
61
+ end
62
+ field :session_data_unpack do
63
+ searchable true
64
+ read_only true
65
+ end
66
+
67
+ if block_given?
68
+ yield self
69
+ end
70
+ }
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,46 @@
1
+ module Hancock
2
+ module Goto
3
+ include Hancock::PluginConfiguration
4
+
5
+ def self.config_class
6
+ Configuration
7
+ end
8
+
9
+ def self.configure
10
+ yield configuration
11
+ end
12
+
13
+ class Configuration
14
+
15
+ attr_accessor :css_selector
16
+ attr_accessor :href_regex
17
+ attr_accessor :excluded_hosts
18
+
19
+ attr_accessor :add_nofollow
20
+ attr_accessor :add_noindex
21
+ attr_accessor :add_noreferrer
22
+ attr_accessor :add_noopener
23
+ attr_accessor :del_attrs
24
+
25
+ attr_accessor :model_settings_support
26
+ attr_accessor :user_abilities_support
27
+ attr_accessor :ra_comments_support
28
+
29
+ def initialize
30
+ @css_selector = "a[href]"
31
+ @href_regex = /^(https?:)?\/\//i
32
+ @excluded_hosts = []
33
+
34
+ @add_nofollow = true
35
+ @add_noindex = true
36
+ @add_noreferrer = true
37
+ @add_noopener = true
38
+ @del_attrs = true
39
+
40
+ @model_settings_support = defined?(RailsAdminModelSettings)
41
+ @user_abilities_support = defined?(RailsAdminUserAbilities)
42
+ @ra_comments_support = defined?(RailsAdminComments)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,30 @@
1
+ module Hancock::Goto
2
+ module Controllers
3
+ module Transfers
4
+ extend ActiveSupport::Concern
5
+
6
+ def index
7
+ url = Addressable::URI.heuristic_parse(params[:url]) rescue nil
8
+ referer = (request.referer ? Addressable::URI.parse(request.referer) : nil) rescue nil
9
+
10
+ @transfer = transfer_class.new
11
+ @transfer.recieved_url = params[:url]
12
+ @transfer.url = url.to_s
13
+ @transfer.host = url.host.to_s if url
14
+ @transfer.referer = referer.to_s
15
+ @transfer.source_ip = request.env['HTTP_X_FORWARDED_FOR'] || request.remote_ip
16
+ if Hancock::Goto.mongoid?
17
+ @transfer.set_session(session)
18
+ end
19
+ @transfer.save
20
+
21
+ redirect_to @transfer.url, code: 303
22
+ end
23
+
24
+ def transfer_class
25
+ Hancock::Goto::Transfer
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,7 @@
1
+ module Hancock
2
+ module Goto
3
+ class Engine < ::Rails::Engine
4
+ # isolate_namespace Hancock::Goto
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,125 @@
1
+ require 'nokogiri'
2
+ require 'addressable'
3
+ require 'rack'
4
+
5
+ module Hancock::Goto
6
+ class Middleware
7
+ include Rack::Utils
8
+
9
+ ATTRS = {
10
+ disabled: 'data-hancock-goto-disabled',
11
+ add_nofollow: 'data-hancock-goto-add-nofollow',
12
+ add_noindex: 'data-hancock-goto-add-noindex',
13
+ add_noreferrer: 'data-hancock-goto-add-noreferrer',
14
+ add_noopener: 'data-hancock-goto-add-noopener',
15
+ del_attrs: 'data-hancock-goto-del-attrs'
16
+ }
17
+
18
+ REL_ATTRS = ATTRS.dup
19
+ REL_ATTRS.delete(:disabled)
20
+ ATTRS.freeze
21
+ REL_ATTRS.freeze
22
+
23
+ def initialize(app, options = {})
24
+ @app = app
25
+ self
26
+ end
27
+
28
+ def call(env)
29
+ status, headers, response = @app.call(env)
30
+ headers = HeaderHash.new(headers)
31
+
32
+ if should_process?(status, headers)
33
+ begin
34
+ _body = response.body
35
+ content = extract_content(response)
36
+ doc = ::Nokogiri::HTML(content)
37
+ array = doc.css(Hancock::Goto.config.css_selector)
38
+
39
+ doc.css(Hancock::Goto.config.css_selector).each do |a|
40
+ if (!a[ATTRS[:disabled]].blank? and !["0", "false", "no"].include?(a[ATTRS[:disabled]]))
41
+ del_attrs(a)
42
+ next
43
+ end
44
+ _href = a['href']
45
+ if _href =~ Hancock::Goto.config.href_regex
46
+ begin
47
+ _host = Addressable::URI.parse(_href).host
48
+ unless Hancock::Goto.config.excluded_hosts.include?(_host)
49
+ a['href'] = Rails.application.routes.url_helpers.hancock_goto_path(url: _href)
50
+ a['target'] = '_blank' if a['target'].blank?
51
+ set_rel_attribute(a)
52
+ del_attrs(a)
53
+ end
54
+ rescue
55
+ end
56
+ end
57
+ end
58
+
59
+ content = doc.to_html
60
+ headers['content-length'] = bytesize(content).to_s
61
+ response = [content]
62
+ rescue Exception => ex
63
+ puts ex.message
64
+ puts ex.backtrace
65
+ end
66
+ end
67
+
68
+ [status, headers, response]
69
+ end
70
+
71
+ private
72
+ def should_process?(status, headers)
73
+ !STATUS_WITH_NO_ENTITY_BODY.include?(status) &&
74
+ !headers['transfer-encoding'] &&
75
+ headers['content-type'] &&
76
+ headers['content-type'].include?('text/html')
77
+ end
78
+
79
+ def extract_content(response)
80
+ response.body
81
+ end
82
+
83
+ def check_attr(a, attr_name)
84
+ Hancock::Goto.config.send(attr_name) or (!a[ATTRS[attr_name]].blank? and !["0", "false", "no"].include?(a[ATTRS[attr_name]]))
85
+ end
86
+
87
+ def add_attr(a, attr_name)
88
+ rel = a['rel'].blank? ? [] : a['rel'].split(" ")
89
+ rel << attr_name unless rel.include?(attr_name)
90
+ a['rel'] = rel.join(" ")
91
+ a
92
+ end
93
+
94
+ def add_nofollow(a)
95
+ add_attr(a, 'nofollow') if check_attr(a, :add_nofollow)
96
+ end
97
+
98
+ def add_noindex(a)
99
+ add_attr(a, 'noindex') if check_attr(a, :add_noindex)
100
+ end
101
+
102
+ def add_noreferrer(a)
103
+ add_attr(a, 'noreferrer') if check_attr(a, :add_noreferrer)
104
+ end
105
+
106
+ def add_noopener(a)
107
+ add_attr(a, 'noopener') if check_attr(a, :add_noopener)
108
+ end
109
+
110
+ def del_attrs(a)
111
+ if check_attr(a, ATTRS[:del_attrs])
112
+ ATTRS.values.each do |_attr|
113
+ a.remove_attribute(_attr)
114
+ end
115
+ end
116
+ end
117
+
118
+ def set_rel_attribute(a)
119
+ REL_ATTRS.keys.each do |meth|
120
+ self.send(meth, a)
121
+ end
122
+ end
123
+
124
+ end
125
+ end
@@ -0,0 +1,39 @@
1
+ module Hancock::Goto
2
+ module Models
3
+ module Mongoid
4
+ module Transfer
5
+ extend ActiveSupport::Concern
6
+ include ::Mongoid::Userstamp
7
+
8
+ included do
9
+ field :recieved_url, type: String
10
+ field :url, type: String
11
+ field :host, type: String
12
+ field :referer, type: String
13
+ field :source_ip, type: String
14
+
15
+ if Hancock.rails4?
16
+ belongs_to :session, class_name: "MongoidStore::Session"
17
+ else
18
+ belongs_to :session, class_name: "MongoidStore::Session", optional: true
19
+ end
20
+ field :session_data, type: BSON::Binary
21
+ end
22
+
23
+ def session_data_extract
24
+ session_data.data if session_data
25
+ end
26
+
27
+ def session_data_unpack
28
+ Marshal.load(session_data_extract) if session_data
29
+ end
30
+
31
+ def set_session(_session)
32
+ self.session_id = _session.id
33
+ self.session_data = BSON::Binary.new(Marshal.dump(_session.to_hash))
34
+ end
35
+
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,39 @@
1
+ module Hancock::Goto
2
+ module Models
3
+ module Transfer
4
+ extend ActiveSupport::Concern
5
+ include Hancock::Model
6
+
7
+ include Hancock::Goto.orm_specific('Transfer')
8
+
9
+ include ManualSlug
10
+
11
+ included do
12
+
13
+ def self.manager_can_default_actions
14
+ [:show, :read].freeze
15
+ end
16
+ def manager_cannot_actions
17
+ [:new, :create, :edit, :update].freeze
18
+ end
19
+
20
+ def self.manager_can_add_actions
21
+ ret = []
22
+ ret << :model_settings if Hancock::Goto.config.model_settings_support
23
+ ret << :model_accesses if Hancock::Goto.config.user_abilities_support
24
+ ret += [:comments, :model_comments] if Hancock::Goto.config.ra_comments_support
25
+ ret.freeze
26
+ end
27
+ def self.rails_admin_add_visible_actions
28
+ ret = []
29
+ ret << :model_settings if Hancock::Goto.config.model_settings_support
30
+ ret << :model_accesses if Hancock::Goto.config.user_abilities_support
31
+ ret += [:comments, :model_comments] if Hancock::Goto.config.ra_comments_support
32
+ ret.freeze
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,15 @@
1
+ module ActionDispatch::Routing
2
+ class Mapper
3
+
4
+ def hancock_cms_goto_routes
5
+
6
+ scope module: 'hancock' do
7
+ scope module: 'goto' do
8
+ get '/goto/*url' => 'transfers#index', as: :hancock_goto, format: false
9
+ end
10
+ end
11
+
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ module Hancock
2
+ module Goto
3
+ VERSION = "1.0.1".freeze
4
+ end
5
+ end
@@ -0,0 +1,34 @@
1
+ require "hancock/goto/version"
2
+
3
+ require "hancock/goto/routes"
4
+
5
+ # require 'hancock_cms'
6
+
7
+ require 'mongoid'
8
+ require 'mongoid_userstamp'
9
+
10
+ require 'hancock/goto/configuration'
11
+ require 'hancock/goto/engine'
12
+ require "hancock/goto/middleware"
13
+
14
+ module Hancock::Goto
15
+ include Hancock::Plugin
16
+
17
+ autoload :Admin, 'hancock/goto/admin'
18
+ module Admin
19
+ autoload :Transfer, 'hancock/goto/admin/transfer'
20
+ end
21
+
22
+ module Models
23
+ autoload :Transfer, 'hancock/goto/models/transfer'
24
+
25
+ module Mongoid
26
+ autoload :Transfer, 'hancock/goto/models/mongoid/transfer'
27
+ end
28
+ end
29
+
30
+ module Controllers
31
+ autoload :Transfers, 'hancock/goto/controllers/transfers'
32
+ end
33
+
34
+ end
data/release.sh ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/bash
2
+ bundle update
3
+ git add --all .
4
+ git commit -am "${*:1}"
5
+ git push -u origin master
6
+ rake release
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hancock_cms_goto
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Kiseliev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: hancock_cms
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ - - "<="
49
+ - !ruby/object:Gem::Version
50
+ version: '2.1'
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '1.0'
58
+ - - "<="
59
+ - !ruby/object:Gem::Version
60
+ version: '2.1'
61
+ - !ruby/object:Gem::Dependency
62
+ name: nokogiri
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :runtime
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: addressable
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :runtime
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ description: URL redirect dispatcher with HancockCMS support.
90
+ email:
91
+ - dev@redrocks.pro
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - ".gitignore"
97
+ - ".ruby-gemset"
98
+ - ".ruby-version"
99
+ - ".travis.yml"
100
+ - Gemfile
101
+ - LICENSE.txt
102
+ - README.md
103
+ - Rakefile
104
+ - app/controllers/concerns/hancock/goto/decorators/transfers.rb
105
+ - app/controllers/hancock/goto/transfers_controller.rb
106
+ - app/models/concerns/hancock/goto/decorators/transfer.rb
107
+ - app/models/hancock/goto/transfer.rb
108
+ - bin/console
109
+ - bin/setup
110
+ - config/locales/hancock.goto.ru.yml
111
+ - hancock_cms_goto.gemspec
112
+ - lib/generators/hancock/goto/config/config_generator.rb
113
+ - lib/generators/hancock/goto/config/templates/hancock_goto.erb
114
+ - lib/generators/hancock/goto/controllers/decorators_generator.rb
115
+ - lib/generators/hancock/goto/controllers/templates/transfers.erb
116
+ - lib/generators/hancock/goto/controllers/transfers_generator.rb
117
+ - lib/generators/hancock/goto/models/decorators_generator.rb
118
+ - lib/generators/hancock/goto/models/templates/transfer.erb
119
+ - lib/generators/hancock/goto/models/transfer_generator.rb
120
+ - lib/hancock/goto/admin.rb
121
+ - lib/hancock/goto/admin/transfer.rb
122
+ - lib/hancock/goto/configuration.rb
123
+ - lib/hancock/goto/controllers/transfers.rb
124
+ - lib/hancock/goto/engine.rb
125
+ - lib/hancock/goto/middleware.rb
126
+ - lib/hancock/goto/models/mongoid/transfer.rb
127
+ - lib/hancock/goto/models/transfer.rb
128
+ - lib/hancock/goto/routes.rb
129
+ - lib/hancock/goto/version.rb
130
+ - lib/hancock_cms_goto.rb
131
+ - release.sh
132
+ homepage: https://github.com/red-rocks/hancock_cms_goto
133
+ licenses:
134
+ - MIT
135
+ metadata: {}
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ requirements: []
151
+ rubyforge_project:
152
+ rubygems_version: 2.5.1
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: URL redirect dispatcher with HancockCMS support.
156
+ test_files: []