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

Active Resource 6.1 made ::all lazy, returning a relation rather than performing the request. We call ::find directly so that a PaginatedCollection is always returned, regardless of the Active Resource version.



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

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

#create(attributes = {}) ⇒ Object



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

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

#create!(attributes = {}) ⇒ Object



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

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

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



46
47
48
49
# File 'lib/fixably/actions.rb', line 46

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

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

Returns:

  • (Boolean)


51
52
53
54
55
56
# File 'lib/fixably/actions.rb', line 51

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

#find(*arguments) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/fixably/actions.rb', line 58

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



68
69
70
71
72
73
74
# File 'lib/fixably/actions.rb', line 68

def first(*arguments)
  ActionPolicy.new(resource: self).list!

  args = arguments.first || {}
  args[:limit] = 1
  super(args)
end

#includes(association) ⇒ Object



76
77
78
# File 'lib/fixably/actions.rb', line 76

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

#last(*arguments) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fixably/actions.rb', line 80

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



92
93
94
95
96
97
# File 'lib/fixably/actions.rb', line 92

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

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