Making mailer functions more DRY [closed] - ruby-on-rails

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Inside a mailer file, I have 11 methods all of which start with the line
#reservation = reservation
Is there a way to make this DRY? I tried the following:
def set_reservation
#reservation = reservation
end
and then
before_action :set_reservation
Unfortunately, this always gave me something along these lines:
AgentReservationMailer#send_reserve_complete_mail: processed outbound mail in 1.7ms
NameError: undefined local variable or method `reservation' for #<AgentReservationMailer:0x007ffc9ae5bb38>
I'm still a very junior level developer, but I would like to try and make things look as professional as I can - is what I am trying to do even possible though?

The reason you're seeing the error is that the mailer does not know of a variable reservation inside the set_reservation method. I'm assuming that the 11 methods you mention, which use the
#reservation = reservation
take reservation as an argument. As it stands, there really is no need to try and reduce duplication.
As a side note, DRY is not a principle you should follow blindly. If you had a couple of lines that were the same in each method, then that would indeed justify an "extract method" refactoring. But replacing that #reservation = reservation assignment with e.g. a method call set_reservation(reservation), you'd still end up repeating one line across all methods.

Related

How to find out the number of records in a table? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a scaffold help requisition. There is a status field. Each request can have 3 statuses (pending, in_progress, finished), implemented through enum. It is necessary to write methods that will count the number of requests with each status. For example, method A counts how many total helper prequests with status 0 (pending). I understand that there is nothing complicated about this, but I can't understand and could use some help.
It would be something like this on your model:
def self.pending_count
where(status: 0).count
end
def self.in_progress_count
where(status: 1).count
end
def self.finished_count
where(status: 2).count
end
Then you can just call Model.pending_count (or the other methods) wherever you need it.

Active Record doing nothing [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
please I need a little help... I don't know what I'm doing wrong but I need just a simple select query with Active Record. This looks my code:
Model:
class Kiosk < ApplicationRecord
#kiosk = Kiosk.all
end
Controller:
class KioskController < ApplicationController
def kiosk
#kiosk = Kiosk.all
end
end
HAML:
##kiosk
And it just doing nothing. Even if I change a password of database there isn't any error with connection. rake db:migrade was done a db was created.
Thanks
You may want to look at your logs (eg Rails.root => logs/development.log) or the output in your terminal – are there any error messages? If you log in to your console with eg rails console and run #kiosks = Kiosk.all – what are you seeing? or how about Kiosk.count –– is it showing that there are any kiosks? As mentioned the model looks funny... not sure why you have the #kiosk = Kiosk.all line in there at all....

ruby on rails beginner: rails for zombies code sample [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
where does that #user in the code in check_ammo come from?
see code for "weapon" in model, controller, mailer and schema.rb
rails for zombies code
That code cant be right.
Weaponmailer
def low_ammo(weapon, zombie)
attachments["weapon.jpg"] = weapon.picture_file
mail to: zombie.email, subject: "#{weapon.name} has low ammo"
end
expects 2 parameters in controller
WeaponMailer.low_ammo(#user).deliver
#user is not defined unless its defined in ActiveRecord::Base what is not the case i think. Its just broken code example

How do I access data in the view from my controller? Rails [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have data I'm getting in my controller and want to display it in the view. I've tried using a helper method but no luck. I've also tried an instance variable but still doesn't work.
You can do so like this :
class SomeClass < ApplicationController
def index
#something = 'This is a cool text'
end
end
In the index.html.erb you can do this :
<%= #something %>

Ruby: more readable way to write nested if conditions? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Is there a more readable way in Ruby (or using Rails model helpers) to write the following:
def get_question
if self.is_question?
self.trackable
elsif self.is_answer?
self.trackable.question
elsif self.is_comment?
if self.trackable.is_question?
self.trackable.commentable
elsif self.trackable.is_answer?
self.trackable.commentable.question
end
end
end
There must be a more "Ruby way" of writing this so it's easier to read for other developers.
I tend to write that sort of thing like this:
def get_question
return self.trackable if self.is_question?
return self.trackable.question if self.is_answer?
return nil if !self.is_comment?
return self.trackable.commentable if self.trackable.is_question?
return self.trackable.commentable.question if self.trackable.is_answer?
return nil
end
that's pretty mu-idiomatic but I don't know if it qualifies as Ruby-idiomatic. Yes, there's an unnecessary return at the end but I like the symmetry and how it makes everything visually line up.
In real life, I'd probably want replace all that logic with a question method on the self.trackable. Then each thing could implement question (or to_question, get_question, or whatever name made the most sense in the broader context):
# Inside questions...
def question
self
end
# Inside answers...
# Nothing special needed, we've already got one.
and so on for the other possible possible self.trackable things. That would leave your get_question looking like this:
def get_question
self.trackable.respond_to?(:question) ? self.trackable.question : nil
end
or you could do away with get_question completely if you knew that self.trackable would also respond to question.

Resources