Sha256: c6865af88343950781c34e9609d0b360f8112c76e91189062d43f47d9fa97ef1

Contents?: true

Size: 997 Bytes

Versions: 3

Compression:

Stored size: 997 Bytes

Contents

module Crystal
  class OptionsForController
    attr_reader :controller

    def initialize(controller)
      @controller = controller
    end

    def options
      paths.inject(default_options){|memo, path| memo.deep_merge!(I18n.t(path, :scope => scope, :default => {}))}
    end

    private

    def paths
      %W{
        _defaults
        #{controller_path}._defaults
        #{controller_path}.#{alias_action}
        #{controller_path}.#{action}
      }.uniq
    end

    def scope
      'meta'
    end

    def default_options
      HashWithStringifyKeys.new(
        :'og:url'      => url,
        :'twitter:url' => url
      )
    end

    def url
      controller.request.url
    end

    def controller_path
      @controller_path ||= controller.controller_path.gsub(%r{/}, '.')
    end

    def action
      controller.action_name
    end

    def alias_action
      aliases[action] || action
    end

    def aliases
      {'update' => 'edit', 'create' => 'new'}
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
crystalmeta-0.9.3 lib/crystal/options_for_controller.rb
crystalmeta-0.9.2 lib/crystal/options_for_controller.rb
crystalmeta-0.9.1 lib/crystal/options_for_controller.rb