Class: BetterController::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/better_controller/service.rb

Overview

Base service class for resource operations

Instance Method Summary collapse

Instance Method Details

#all(ancestry_params = {}) ⇒ ActiveRecord::Relation

Get all resources

Parameters:

  • ancestry_params (Hash) (defaults to: {})

    Ancestry parameters for nested resources

Returns:

  • (ActiveRecord::Relation)

    Collection of resources



9
10
11
# File 'lib/better_controller/service.rb', line 9

def all(ancestry_params = {})
  resource_scope(ancestry_params).all
end

#create(ancestry_params = {}, attributes) ⇒ Object

Create a new resource

Parameters:

  • ancestry_params (Hash) (defaults to: {})

    Ancestry parameters for nested resources

  • attributes (Hash)

    The resource attributes

Returns:

  • (Object)

    The created resource



25
26
27
28
29
30
31
# File 'lib/better_controller/service.rb', line 25

def create(ancestry_params = {}, attributes)
  resource = resource_class.new(prepare_attributes(attributes, ancestry_params))

  resource.save if resource.respond_to?(:save)

  resource
end

#destroy(ancestry_params = {}, id) ⇒ Object

Destroy a resource

Parameters:

  • ancestry_params (Hash) (defaults to: {})

    Ancestry parameters for nested resources

  • id (Integer, String)

    The resource ID

Returns:

  • (Object)

    The destroyed resource



50
51
52
53
54
55
56
# File 'lib/better_controller/service.rb', line 50

def destroy(ancestry_params = {}, id)
  resource = find(ancestry_params, id)

  resource.destroy if resource.respond_to?(:destroy)

  resource
end

#find(ancestry_params = {}, id) ⇒ Object

Find a specific resource

Parameters:

  • ancestry_params (Hash) (defaults to: {})

    Ancestry parameters for nested resources

  • id (Integer, String)

    The resource ID

Returns:

  • (Object)

    The found resource



17
18
19
# File 'lib/better_controller/service.rb', line 17

def find(ancestry_params = {}, id)
  resource_scope(ancestry_params).find(id)
end

#update(ancestry_params = {}, id, attributes) ⇒ Object

Update an existing resource

Parameters:

  • ancestry_params (Hash) (defaults to: {})

    Ancestry parameters for nested resources

  • id (Integer, String)

    The resource ID

  • attributes (Hash)

    The resource attributes

Returns:

  • (Object)

    The updated resource



38
39
40
41
42
43
44
# File 'lib/better_controller/service.rb', line 38

def update(ancestry_params = {}, id, attributes)
  resource = find(ancestry_params, id)

  resource.update(prepare_attributes(attributes, ancestry_params)) if resource.respond_to?(:update)

  resource
end