Sha256: e2a29df2a259bddf90a16fa9eeca90420e4cae84f48d137f5da89accb50b519c

Contents?: true

Size: 1.73 KB

Versions: 2

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module Satis
  class Configuration
    attr_accessor :submit_on_enter, :confirm_before_leave
    attr_writer :default_help_text
    attr_writer :logger

    def initialize
      @logger = Logger.new(STDOUT)
      @submit_on_enter = true
      @confirm_before_leave = false

      @default_help_text = lambda do |_template, _object, key, _additional_scope|
        scope = help_scope(template, object, additional_scope)

        value = I18n.t((["help"] + scope + [key.to_s]).join("."))

        if /translation missing: (.+)/.match?(value)
          nil
        else
          value
        end
      end
    end

    # Config: logger [Object].
    def logger
      @logger.is_a?(Proc) ? instance_exec(&@logger) : @logger
    end

    def default_help_text(template, object, method, additional_scope)
      if @default_help_text.is_a?(Proc)
        instance_exec(template, object, method, additional_scope,
          &@default_help_text)
      else
        @default_help_text
      end
    end

    # Maybe not the right place?
    def help_scope(template, object, additional_scope, action: nil)
      scope = template.controller.controller_path.split("/")
      scope << (action || template.controller.action_name)
      scope << object.class.name.demodulize.tableize.singularize

      scope += Array.wrap(additional_scope) if additional_scope

      scope.map(&:to_s)
    end

    def help_scopes(template, object, additional_scope)
      actions = [template.controller.action_name]
      %w[show new edit create update destroy index].each do |action|
        actions << action unless actions.include?(action)
      end

      actions.map { |action| help_scope(template, object, additional_scope, action: action) }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
satis-1.0.67 lib/satis/configuration.rb
satis-1.0.66 lib/satis/configuration.rb