Sha256: d70dacf74210ca887c29ad65e8b6ac135621b5d6acdfd6c0b93c385a1eead30c

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

module RailsAmp
  module Overrider
    extend ActiveSupport::Concern

    included do
      before_action do
        RailsAmp.format = request[:format]
        if RailsAmp.amp_renderable?(controller_path, action_name)  # default_format is :amp
          override_actions_with_rails_amp
        end
      end
    end

    private

      def override_actions_with_rails_amp
        klass = self.class  # klass is controller class
        return if klass.ancestors.include?(RailsAmp::ActionOverrider)
        actions = RailsAmp.target_actions(klass)

        klass.class_eval do
          # override amp target actions
          RailsAmp::ActionOverrider.module_eval do
            actions.to_a.each do |action|
              define_method action.to_sym do
                super()
                unless performed?
                  respond_to do |format|
                    format.send(RailsAmp.default_format.to_sym) do
                      # search amp format(default is .amp) .html templates
                      lookup_context.formats = [RailsAmp.default_format] + RailsAmp.lookup_formats
                      render layout: 'rails_amp_application.amp'
                    end
                  end
                end
              end
            end
          end
          prepend RailsAmp::ActionOverrider
        end
      end
  end

  module ActionOverrider
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails_amp-0.2.2 lib/rails_amp/overrider.rb
rails_amp-0.2.1 lib/rails_amp/overrider.rb