module Fixably::Actions::ClassMethods

Public Instance Methods

actions(values = nil) click to toggle source
# File lib/fixably/actions.rb, line 16
def actions(values = nil)
  if eql?(ApplicationResource)
    raise "actions can only be called on a sub-class"
  end

  @actions ||= [].freeze
  return @actions if values.nil?

  @actions = format_actions(values).freeze
end
all(*arguments) click to toggle source
Calls superclass method
# File lib/fixably/actions.rb, line 27
def all(*arguments)
  ActionPolicy.new(resource: self).list!
  super(*arguments)
end
create(attributes = {}) click to toggle source
Calls superclass method
# File lib/fixably/actions.rb, line 32
def create(attributes = {})
  ActionPolicy.new(resource: self).create!
  super(attributes)
end
create!(attributes = {}) click to toggle source
Calls superclass method
# File lib/fixably/actions.rb, line 37
def create!(attributes = {})
  ActionPolicy.new(resource: self).create!
  super(attributes)
end
delete(id, options = {}) click to toggle source
Calls superclass method
# File lib/fixably/actions.rb, line 42
def delete(id, options = {})
  ActionPolicy.new(resource: self).delete!
  super(id, options)
end
exists?(id, options = {}) click to toggle source
# File lib/fixably/actions.rb, line 47
def exists?(id, options = {})
  find(id, options)
  true
rescue ::ActiveResource::ResourceNotFound
  false
end
find(*arguments) click to toggle source
Calls superclass method
# File lib/fixably/actions.rb, line 54
def find(*arguments)
  scope = arguments.slice(0)

  ActionPolicy.new(resource: self).show! unless scope.instance_of?(Symbol)

  args = parametize_arguments(scope, arguments.slice(1))

  super(scope, args)
end
first(*arguments) click to toggle source
Calls superclass method
# File lib/fixably/actions.rb, line 64
def first(*arguments)
  ActionPolicy.new(resource: self).list!

  args = arguments.first || {}
  args[:limit] = 1
  super(args)
end
includes(association) click to toggle source
# File lib/fixably/actions.rb, line 72
def includes(association)
  ResourceLazyLoader.new(model: self).includes(association)
end
last(*arguments) click to toggle source
Calls superclass method
# File lib/fixably/actions.rb, line 76
def last(*arguments)
  ActionPolicy.new(resource: self).list!

  args = parametize_arguments(:last, arguments.first)
  collection = find_every(args)
  return collection.last unless collection.offset.zero?
  return collection.last if collection.total_items <= collection.limit

  args = args[:params].merge(limit: 1, offset: collection.total_items - 1)
  super(args)
end
where(clauses = {}) click to toggle source
# File lib/fixably/actions.rb, line 88
def where(clauses = {})
  ActionPolicy.new(resource: self).list!

  arguments = stringify_array_values(clauses)
  find(:all, arguments)
end

Private Instance Methods

allowed_actions(= %i[create delete list show update]) click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/fixably/actions.rb, line 118
  def allowed_actions = %i[create delete list show update]
end
destroy() click to toggle source
Calls superclass method
# File lib/fixably/actions.rb, line 121
def destroy
  ActionPolicy.new(resource: self).delete!
  super()
end
format_actions(values) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/fixably/actions.rb, line 98
def format_actions(values)
  unless values.respond_to?(:to_sym) || values.respond_to?(:to_a)
    raise(
      ArgumentError,
      "actions should be able to be converted into an Array or a Symbol"
    )
  end

  Array.wrap(values).map do
    action = _1.to_sym

    unless allowed_actions.include?(action)
      raise ArgumentError, "Unsupported action, #{action}, supplied"
    end

    action
  end
end
save(validate: true) click to toggle source
Calls superclass method
# File lib/fixably/actions.rb, line 126
def save(validate: true)
  if validate
    message = new? ? :create! : :update!
    ActionPolicy.new(resource: self).public_send(message)
  end

  super()
end
save!() click to toggle source

rubocop:disable Style/RaiseArgs

# File lib/fixably/actions.rb, line 136
def save!
  if new?
    ActionPolicy.new(resource: self).create!
  else
    ActionPolicy.new(resource: self).update!
  end

  save(validate: false) ||
    raise(::ActiveResource::ResourceInvalid.new(self))
end