Sha256: e6c30f707cdd42ad9c512edd178bbf0ee826b0f9b45a1eb58ce7a81ab27f876e

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 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()
                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
          prepend RailsAmp::ActionOverrider
        end
      end
  end

  module ActionOverrider
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_amp-0.2.0 lib/rails_amp/overrider.rb