Sha256: cf851efdd35cf7be7b9a5ca9079beb910945facb7261c674fb306efb0356688c

Contents?: true

Size: 1.54 KB

Versions: 2

Compression:

Stored size: 1.54 KB

Contents

require 'locomotive_plugins'
require 'locomotive/page_redirect/plugin/config'

module Locomotive
  module PageRedirect
    class Plugin
      include Locomotive::Plugin

      after_plugin_setup :set_config
      before_page_render :redirect

      def config_template_file
        File.join(File.dirname(__FILE__), 'plugin', 'config.haml')
      end

      protected

      def set_config
        Config.hash = config
      end

      # gets the current site
      def get_site
        controller.send(:current_site)
      end

      # gets the model and it needs for the redirects
      def get_model(model_slug)
        site = get_site
        site.content_types.where(:slug => model_slug).first
      end

      # loops through all the redirect entries from the given model
      # and checks if the fullpath match the regex given and will
      # redirect to the given url.
      def redirect
        redirect_model = get_model(Config.hash[:redirect_model_slug])
        if redirect_model
          redirect_model.entries.each do |entry|
            regexp = entry.send(Config.hash[:regexp_field_slug])
            redirect_url = entry.send(Config.hash[:url_field_slug])
            if controller.request.fullpath =~ /#{regexp}/
              regexp_data = /#{regexp}/.match(controller.request.fullpath)
              /#{regexp}/.named_captures.each do |key,value|
              redirect_url.gsub!(/\$#{key}/, regexp_data[key])
              end
            controller.redirect_to redirect_url and return
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
locomotive_page_redirect_plugin-0.1.1 lib/locomotive/page_redirect/plugin.rb
locomotive_page_redirect_plugin-0.1.0 lib/locomotive/page_redirect/plugin.rb