Sha256: c0937effd866a9d68ff05abfa2dacdb73d0f3e5ca2d9390f0c1db8afc3ba5d2d

Contents?: true

Size: 1.28 KB

Versions: 6

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_name, 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

6 entries across 6 versions & 1 rubygems

Version Path
rails_amp-0.1.9 lib/rails_amp/overrider.rb
rails_amp-0.1.8 lib/rails_amp/overrider.rb
rails_amp-0.1.7 lib/rails_amp/overrider.rb
rails_amp-0.1.6 lib/rails_amp/overrider.rb
rails_amp-0.1.5 lib/rails_amp/overrider.rb
rails_amp-0.1.4 lib/rails_amp/overrider.rb