Module: TypicalSituation::Operations
- Included in:
- TypicalSituation
- Defined in:
- lib/typical_situation/operations.rb
Overview
Model operations. Assume that we’re working w/ an ActiveRecord association collection.
Instance Method Summary collapse
- #assign_resource(resource, attrs) ⇒ Object
- #build_resource(attrs) ⇒ Object
- #create_resource(attrs) ⇒ Object
- #default_sorting_attribute ⇒ Object
- #default_sorting_direction ⇒ Object
- #destroy_resource(resource) ⇒ Object
- #find_resource(param) ⇒ Object
- #get_resource ⇒ Object
- #get_resources ⇒ Object
- #has_errors? ⇒ Boolean
- #model_class ⇒ Object
- #new_resource ⇒ Object
- #paginate_resources(resources) ⇒ Object
- #pagination_params ⇒ Object
- #scoped_resource ⇒ Object
- #serializable_resource(resource) ⇒ Object
- #serialize_resource(resource, options = {}) ⇒ Object
- #serialize_resources(resources) ⇒ Object
-
#set_collection_instance ⇒ Object
Set the plural instance variable named after the model.
-
#set_single_instance ⇒ Object
Set the singular instance variable named after the model.
- #update_resource(resource, attrs) ⇒ Object
Instance Method Details
#assign_resource(resource, attrs) ⇒ Object
58 59 60 |
# File 'lib/typical_situation/operations.rb', line 58 def assign_resource(resource, attrs) resource.assign_attributes(attrs) end |
#build_resource(attrs) ⇒ Object
72 73 74 |
# File 'lib/typical_situation/operations.rb', line 72 def build_resource(attrs) @resource = collection.build(attrs) end |
#create_resource(attrs) ⇒ Object
68 69 70 |
# File 'lib/typical_situation/operations.rb', line 68 def create_resource(attrs) @resource = collection.create(attrs) end |
#default_sorting_attribute ⇒ Object
15 16 17 |
# File 'lib/typical_situation/operations.rb', line 15 def default_sorting_attribute nil end |
#default_sorting_direction ⇒ Object
19 20 21 |
# File 'lib/typical_situation/operations.rb', line 19 def default_sorting_direction :asc end |
#destroy_resource(resource) ⇒ Object
62 63 64 65 66 |
# File 'lib/typical_situation/operations.rb', line 62 def destroy_resource(resource) resource.destroy collection.reload if resource.errors.empty? resource end |
#find_resource(param) ⇒ Object
11 12 13 |
# File 'lib/typical_situation/operations.rb', line 11 def find_resource(param) find_in_collection(param) end |
#get_resource ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/typical_situation/operations.rb', line 31 def get_resource if (@resource = find_resource(id_param)) set_single_instance @resource else raise ActiveRecord::RecordNotFound, "Could not find #{model_class}( id:#{id_param.inspect} )" end end |
#get_resources ⇒ Object
44 45 46 47 48 |
# File 'lib/typical_situation/operations.rb', line 44 def get_resources @resources = paginate_resources(apply_sorting(scoped_resource)) set_collection_instance @resources end |
#has_errors? ⇒ Boolean
40 41 42 |
# File 'lib/typical_situation/operations.rb', line 40 def has_errors? !@resource.errors.empty? end |
#model_class ⇒ Object
76 77 78 |
# File 'lib/typical_situation/operations.rb', line 76 def model_class @model_class ||= model_type.to_s.camelize.constantize end |
#new_resource ⇒ Object
50 51 52 |
# File 'lib/typical_situation/operations.rb', line 50 def new_resource @resource = collection.build end |
#paginate_resources(resources) ⇒ Object
23 24 25 |
# File 'lib/typical_situation/operations.rb', line 23 def paginate_resources(resources) resources end |
#pagination_params ⇒ Object
27 28 29 |
# File 'lib/typical_situation/operations.rb', line 27 def pagination_params params.permit(:page, :per_page) end |
#scoped_resource ⇒ Object
7 8 9 |
# File 'lib/typical_situation/operations.rb', line 7 def scoped_resource collection end |
#serializable_resource(resource) ⇒ Object
92 93 94 |
# File 'lib/typical_situation/operations.rb', line 92 def serializable_resource(resource) resource end |
#serialize_resource(resource, options = {}) ⇒ Object
80 81 82 |
# File 'lib/typical_situation/operations.rb', line 80 def serialize_resource(resource, = {}) serializable_resource(resource).to_json(.merge(root: include_root?)) end |
#serialize_resources(resources) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/typical_situation/operations.rb', line 84 def serialize_resources(resources) if include_root? return {plural_model_type => serializable_resource(resources)} end serializable_resource(resources).to_json(root: false) end |
#set_collection_instance ⇒ Object
Set the plural instance variable named after the model. Modules are delimited with “_”. Example: a MockApplePie resource collection is set to ivar @mock_apple_pies.
104 105 106 |
# File 'lib/typical_situation/operations.rb', line 104 def set_collection_instance instance_variable_set(:"@#{model_type.to_s.gsub("/", "__").pluralize}", @resources) end |
#set_single_instance ⇒ Object
Set the singular instance variable named after the model. Modules are delimited with “_”. Example: a MockApplePie resource is set to ivar @mock_apple_pie.
98 99 100 |
# File 'lib/typical_situation/operations.rb', line 98 def set_single_instance instance_variable_set(:"@#{model_type.to_s.gsub("/", "__")}", @resource) end |
#update_resource(resource, attrs) ⇒ Object
54 55 56 |
# File 'lib/typical_situation/operations.rb', line 54 def update_resource(resource, attrs) resource.update(attrs) end |