Volt Framework Task returns string - voltrb

I am currently working on a sports app.
Users should be able to create new teams and add users to the teams.
When a user creates the team, he/she should be able to search for users to add to the team, however the search should only return users that are not yet part of the team.
I created a task for it, here is the method
def search_users_for_team(team_id, search_item)
available_users = Array.new
temp_users = store._users.where(name: { '$regex' => /.*#{search_item}.*/, '$options' => 'i' }).all
team_users = store._teams.where(id: team_id).first._members
team_users_ids = []
team_users.each{ |user|
team_users_ids << user.id
}
temp_users.each{ |user|
if !team_users_ids.include?(user.id)
available_users << user
end
}
available_users
end
However i always get a string back from the function.
Did i miss something about tasks in volt.
I use volt 0.9.4.
Regards,
Kevin

Related

How do I access submitted params in a rails mailer?

I had a previous question that helped me loop through all users where a certain question is met.
However, I'm realizing I can't hard code that condition. I need to somehow get that data from the submitted form, which doesn't seem to be possible in the mailer.
In other words, I'm trying to loop through all users where the user's state is equal to the home_state of the candidate being entered. Basically when the candidate is created, I want to get the home_state of that candidate, and then loop through all users, and for each user that has same state as that candidate, I want to send them the email via this mailer.
Here's my candidate_mailer.rb file
class CandidateMailer < ApplicationMailer
default from: 'wesleycreations#gmail.com'
def self.send_request(row)
#candidate = Candidate.new(candidate_params) # if I can access this here, how to I create the
# following array?
emails = []
User.where(state: #candidate.home_state).each do |u|
emails << u.email # To insert the user email into the array
end
emails.each do |email|
new_request(email,row).deliver_now
end
end
def new_request(email, row)
#candidate = row
mail(to: email, subject: 'New request')
end
end
But the
#candidate = Candidate.new(candidate_params)
obviously doesn't work because the params aren't available in the mailer.
Here in the candidates_controller.rb I have this
def create
#candidate = Candidate.new(candidate_params) #of course here I can access params
if #candidate.save
row = #candidate
CandidateMailer.send_request(row)
else
render('new')
end
end
SO the question is, how do I access params in rails mailer? And if I can't, then how do I refactor my code so that the lines that check if the user meets certain condition is done in the controller?
I was able to figure this out by doing this. after I saved the candidate, I saved the candidate to a global variable. and THEN I send the mailer.
def create
#candidate = Candidate.new(candidate_params)
if #candidate.save
row = #candidate
$candidate = #candidate
end
CandidateMailer.send_request(row)
else
end
end
This way the mailer had access to the new candidate that been created, and I was able to check my condition in there.
So in my mailer, when I use $candidate.home_state, it returned the correct state, mail went out, and made me very happy :)
emails = []
User.where(state: $candidate.home_state).each do |u|
emails << u.email # To insert the user email into the array
end

How can I retrieve a full list of companies (advertisers) from the Google Ad Manager API?

I'm building a rails 5 API that interacts with Google Ad Manager API and I need a way to retrieve the list of companies (also called advertisers) so I can use them in order to create new orders and eventually, line items.
I haven't been able to find this in the Ad Manager documentation, not for ruby and not for other languages either.
I don't have any code yet since I don't know what service to use.
I'd expect an array of advertisers: [{:name => 'Company 1', :id => '1234'}, {:name => 'Company 2', :id => '4321'}]
Any help would be really appreciated. I haven't been able to find this in the Ad Manager documentation, not for ruby and not for other languages either.
Thanks in advance!
I have found the answer. I was wrong, it is part of the Ruby examples:
def get_advertisers(ad_manager)
company_service = ad_manager.service(:CompanyService, API_VERSION)
# Create a statement to select companies.
statement = ad_manager.new_statement_builder do |sb|
sb.where = 'type = :type'
sb.with_bind_variable('type', 'ADVERTISER')
end
# Retrieve a small amount of companies at a time, paging
# through until all companies have been retrieved.
page = {:total_result_set_size => 0}
begin
# Get the companies by statement.
page = company_service.get_companies_by_statement(
statement.to_statement()
)
# Print out some information for each company.
unless page[:results].nil?
page[:results].each_with_index do |company, index|
puts '%d) Company with ID %d, name "%s", and type "%s" was found.' %
[index + statement.offset, company[:id], company[:name],
company[:type]]
end
end
# Increase the statement offset by the page size to get the next page.
statement.offset += statement.limit
end while statement.offset < page[:total_result_set_size]
puts 'Total number of companies: %d' % page[:total_result_set_size]
end

How does one get a list of facebook campaign impressions and budget per campaign with ruby?

I have the following code that returns a list of names of all campaigns that I have in my Facebook business manager account using facebook-ruby-business-sdk.
require 'facebookbusiness'
FacebookAds.configure do |config|
config.access_token = '_____private_info______'
config.app_secret = '_____private_info______'
end
ad_account = FacebookAds::AdAccount.get('act______private_info______', 'name')
puts "Ad Account Name: #{ad_account.name}"
# Printing all campaign names
ad_account.campaigns(fields: 'name').each do |campaign|
puts campaign.name
end
The campaign object in the final loop is an instance of FacebookAds::Campaign.
Rather than just having a list of names, I also need budget and impressions for each campaign in the last month.
How do I need to change the code to get these results?
It looks to me as if you just need to select the fields and they'll be available in your loop:
ad_account.campaigns(fields: %w[name budget impressions]).each do |campaign|
puts [campaign.name, campaign.budget, campaign.impressions].join(', ')
end
Does that work for you? Let me know how you get on or if you have any questions.

Update users table when running create action of different model

Just to show you how I got to this point:
Every user has many profiles. Every profile has type recognized by single table inheritance(amateur, professional, and some other). I need to store current_profile somewhere and somehow.
Professionals Controller
class ProfessionalsController < ApplicationController
def create
#professional = Professional.new(professional_params)
#user = current_user
#professional.user_id = current_user.id
#update_current_profile = User.update(#user, {:current_profile => #professional.id})
if #professional.save
...
else
...
end
end
private
def professional_params
params.require(:professional).permit(:id, :username, :user_id)
end
end
This is meant to update current_profile of user to the newly created professional profile and do some staff then.
When the profile is created current_profile is set(updated) to NULL. If I change :
#update_current_profile = User.update(#user, {:current_profile => #professional.id})
to something different, for example:
#update_current_profile = User.update(#user, {:current_profile => #professional.user_id})
or
#update_current_profile = User.update(#user, {:current_profile => 3})
it stores data in User.current_profile perfectly.
I was trying #professional without an .id too. Why is this doing so?
Another question is. Is this the best way to store current_profile of user? Would you recommend me any better/safer/more efficient solution?
Thanks all of you guys.
#professional is a new, unsaved record and therefore does not have an id yet. Can only update the current_profile once the #professional record was saved.
Just reorder the lines a bit and it should work:
def create
#professional = Professional.new(professional_params)
#user = current_user
#professional.user_id = current_user.id
if #professional.save
#update_current_profile = User.update(#user, {:current_profile => #professional.id})
# ...
else
# ...
end
end
Just another tip: You use many instance variables (the one with the #) in this method. I do not know enough about your code, but I would suggest to look if some of them can be replaced with local variables. Rule of thumb: Only use instance variables in controllers when you want to share that variable with another method or the view.

How do I create English-language list (name1, name2, AND name3) in Ruby on Rails?

Quite possibly there could be a rails magic I've missed, but I'm guessing it will be in Ruby.
I have a model called Company which has_many Contacts.
Suppose Company has Contact 1, Contact 2, Contact 3, and Contact 4.
When I create a textblog for each Contact, I want to output the following (where Contact = Contact 1)
"Hi, Contact 1,
I am also writing to Contact 2, Contact 3, and Contact 4."
So it needs to extract the Contact in the salutation and then list them, inserting "and" before the last Contact in the list.
Edit
I now have the following:
This is in contacts_controller.rb
def full_name
[self.first_name, self.last_name].compact.join(" ")
end
in contact_letters_controllers.rb (contact_letters are the executed contacts with letters)
def new
#contact_letter = ContactLetter.new
#contact_letter.contact_id = params[:contact]
#contact_letter.letter_id = params[:letter]
#contact = Contact.find(params[:contact])
#company = Company.find(#contact.company_id)
contacts = #company.contacts.collect(&:full_name)
contacts.each do |contact|
#colleagues = contacts.reject{ |c| c==#contact.full_name }
end
#letter = Letter.find(#contact_letter.letter_id)
#letter.body.gsub!("{FirstName}", #contact.first_name)
#letter.body.gsub!("{Company}", #contact.company_name)
#letter.body.gsub!("{Colleagues}", #colleagues.to_sentence)
#contact_letter.body = #letter.body
#contact_letter.status = "sent"
end
I believe to_sentence is what you're looking for.
You could try something like the following:
contacts = Company.contacts.collect(&:name)
contacts.each do |contact|
other_contacts = contacts.reject{ |c| c==contact }
p "Hi, #{contact}, I am also writing to #{other_contacts.to_sentence}"
end
I didn't test this code, so forgive me if this doesn't work.
Edit
This method will return the contact's full_name. If you also have a middle_name attribute, you can add on the array. This method will still work if any of the attributes is nil.
def full_name
[self.first_name, self.last_name].compact.join(" ")
end

Resources