Module: Fixably::Actions::ClassMethods

Includes:
Fixably::ArgumentParameterisation
Defined in:
lib/fixably/actions.rb

Instance Method Summary collapse

Instance Method Details

#actions(values = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# 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) ⇒ Object



27
28
29
30
# File 'lib/fixably/actions.rb', line 27

def all(*arguments)
  ActionPolicy.new(resource: self).list!
  super(*arguments)
end

#create(attributes = {}) ⇒ Object



32
33
34
35
# File 'lib/fixably/actions.rb', line 32

def create(attributes = {})
  ActionPolicy.new(resource: self).create!
  super(attributes)
end

#create!(attributes = {}) ⇒ Object



37
38
39
40
# File 'lib/fixably/actions.rb', line 37

def create!(attributes = {})
  ActionPolicy.new(resource: self).create!
  super(attributes)
end

#delete(id, options = {}) ⇒ Object



42
43
44
45
# File 'lib/fixably/actions.rb', line 42

def delete(id, options = {})
  ActionPolicy.new(resource: self).delete!
  super(id, options)
end

#exists?(id, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
# File 'lib/fixably/actions.rb', line 47

def exists?(id, options = {})
  find(id, options)
  true
rescue ::ActiveResource::ResourceNotFound
  false
end

#find(*arguments) ⇒ Object



54
55
56
57
58
59
60
61
62
# 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) ⇒ Object



64
65
66
67
68
69
70
# 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) ⇒ Object



72
73
74
# File 'lib/fixably/actions.rb', line 72

def includes(association)
  ResourceLazyLoader.new(model: self).includes(association)
end

#last(*arguments) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# 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 = {}) ⇒ Object



88
89
90
91
92
93
# File 'lib/fixably/actions.rb', line 88

def where(clauses = {})
  ActionPolicy.new(resource: self).list!

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