Rails Flashcards

1
Q

Rails concerns

A

The Concern is a tool provided by the ActiveSupport lib for including modules in classes, creating mixins.

Concerns are modules that get mixed into controller or model classes for instance. DHH’s post focused on models, but the same applies to controllers. It helps slim down the model or controller classes, and makes it easier to reuse common code across multiple classes.

https://medium.com/@heyamberwilkie/model-helpers-in-rails-concerns-6f5db8d0a59e

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Service objects

A

Service Objects should encapsulate one of the responsibilities of the business logic, provide an entry point for external services, or act as an alternative to model concerns.

When your codebase starts growing, you’ll run into scenarios where the conventional Fat Model, Skinny Controller mantra breaks. When your business logic can’t fit into either a model or a controller, that’s when service objects come in and let us separate every business action into its own Ruby object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Difference between Procs and Lambdas

A

Procs don’t care about the correct number of arguments, while lambdas will raise an exception.
Return and break behaves differently in procs and lambdas
Next behaves same way in both procs and lambdas

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Models

A

Models should handle persistence, associations and not much else.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Controllers

A

Controllers should handle user requests and be a wrapper around the business logic (Service Objects).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Helpers

A

Helpers are for short presentation-oriented methods that you want to call from your views, or very occasionally from your controllers, maybe.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

“module builder pattern”.

A

This technique relies on two primary features of Ruby:

1)  Modules are just like any other objects—they can be created on the fly, assigned to variables, dynamically modified, as well as passed to or returned from methods.
def make_module
     # create a module on the fly and assign it to variable
     mod = Module.new
     # modify module
     mod.module_eval do
       def hello
         "Hello, AppSignal world!"
       end
     end
 # explicitly return it
 mod    end

2) The argument to include or extend calls doesn’t have to be a module, it can also be an expression returning one, e.g. a method call.
class Test
# include the module returned by make_module
include make_module
end

   Test.new.hello
   #=> "Hello, AppSignal world!"

https://blog.appsignal.com/2019/11/29/configurable-ruby-modules-the-module-builder-pattern.html

How well did you know this?
1
Not at all
2
3
4
5
Perfectly