Sha256: c37f0640cb9b5ee66680d5a41ed13c928ed567afb2bd782c246e5fd22becb728

Contents?: true

Size: 1.24 KB

Versions: 4

Compression:

Stored size: 1.24 KB

Contents

module Riveter
  module Command
    extend ActiveSupport::Concern

    included do
      include Riveter::Attributes

      class << self
        alias_method :command_name, :model_name

        def i18n_scope
          :commands
        end

        def success_notice
          I18n.translate(
            :success,
            :scope => [i18n_scope, :notices, command_name.i18n_key],
            :default => "Successfully executed #{command_name.human}."
          )
        end
      end

      alias_method :can_perform?, :valid?
    end

    def submit(*args)
      params = args.extract_options!

      # filter and clean params before applying
      apply_params(
        clean_params(
          filter_params(params)
        )
      )

      # perform validations, and proceed if valid
      return false unless self.can_perform?

      # all good, perform the action
      self.perform(*args)
    end

  protected

    def perform(*args)
      # resolve for the registered service for this command
      service_class = Service::Base.resolve!(self.class)
      # create an instance and invoke perform
      service = service_class.new()
      service.perform(self, *args)
    end

    # helper class
    class Base
      include Command
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
riveter-0.0.13 lib/riveter/command.rb
riveter-0.0.12 lib/riveter/command.rb
riveter-0.0.11 lib/riveter/command.rb
riveter-0.0.10 lib/riveter/command.rb