Module: Upgrow::ActiveRecordQueries

Included in:
Repository
Defined in:
lib/upgrow/active_record_queries.rb

Overview

Mixin that implements Repository methods with an Active Record Base. When included in a Repository class, it sets the default base to be a class ending with ‘Record`.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



27
28
29
# File 'lib/upgrow/active_record_queries.rb', line 27

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#allArray<Model>

Fetches all Records and returns them as an Array of Models.

Returns:

  • (Array<Model>)

    a collection of Models representing all persisted Records.



35
36
37
# File 'lib/upgrow/active_record_queries.rb', line 35

def all
  to_model(base.all)
end

#create(input) ⇒ Model

Persists a new Record with the given input, and materializes the newly created Record as the returned Model instance.

Parameters:

  • input (Input)

    the Input with the attributes for the new Record.

Returns:

  • (Model)

    the Model with the attributes of the newly created Record.



46
47
48
49
# File 'lib/upgrow/active_record_queries.rb', line 46

def create(input)
  record = base.create!(input.attributes)
  to_model(record)
end

#delete(id) ⇒ Object

Deletes the Record that has the given ID.

Parameters:

  • id (Integer)

    the ID of the Record to be deleted.



77
78
79
# File 'lib/upgrow/active_record_queries.rb', line 77

def delete(id)
  base.destroy(id)
end

#find(id) ⇒ Model

Retrieves the Record with the given ID, representing its data as a Model.

Parameters:

  • id (Integer)

    the ID of the Record to be fetched.

Returns:

  • (Model)

    the Model with the attributes of the Record with the given ID.



57
58
59
60
# File 'lib/upgrow/active_record_queries.rb', line 57

def find(id)
  record = base.find(id)
  to_model(record)
end

#update(id, input) ⇒ Model

Updates the Record with the given ID with the given Input attributes.

Parameters:

  • id (Integer)

    the ID of the Record to be updated.

  • input (Input)

    the Input with the attributes to be set in the Record.

Returns:

  • (Model)

    the Model instance with the updated data of the Record.



69
70
71
72
# File 'lib/upgrow/active_record_queries.rb', line 69

def update(id, input)
  record = base.update(id, input.attributes)
  to_model(record)
end