Simple fields for performance on table with 230 rows - simple-form

We are trying to embed two simple form fields as columns in a table, we noticed that it takes about 4.5 seconds for simple fields to generate those tags. The table has 230 rows.
Performance with the simple_fields_for block commented out is .5 seconds, with simple fields for : 5 seconds
= simple_form_for :account,url: create_transactions_path, method: :put do |f|
%table.table.table-striped
......
........
........
%tbody
- loans_view = loans_view(#loans)
- loans_view.each do |lv|
- loan = lv[:loan]
- account = loan.account
.......
.......
%td
= lv[:amount_due]
= f.simple_fields_for :loan, index: loan.id do |al_f|
= al_f.simple_fields_for :account_transaction, index: account.id do |act_f|
%td
= act_f.input :amount, label: false, input_html:{value: account.top_up_amount}
%td
= act_f.input :include_for_update, as: :boolean, label: false, input_html: {checked: true}
We had enabled logging and made sure no db call goes out or any time consuming API is being called in the simple fields for block.

Related

Problem matching amounts with PaypalExpressGateway.setup_authorization

I'm using ruby client with activemerchant to create call for Paypal sandbox API. Script is being called from command line, so variables are filled with command line parameters. Here's the code sample:
login = ARGV[0]
password = ARGV[1]
signature = ARGV[2]
ip = ARGV[3]
subtotal = ARGV[4]
shipping = ARGV[5]
handling = ARGV[6]
tax = ARGV[7]
currency = ARGV[8]
return_url = ARGV[9]
cancel_return_url = ARGV[10]
allow_guest_checkout = ARGV[11]
items = JSON.parse(ARGV[12])
ActiveMerchant::Billing::Base.mode = :test
paypal_options = {
login: login,
password: password,
signature: signature
}
gateway = ActiveMerchant::Billing::PaypalExpressGateway.new(paypal_options)
setup_hash = {
ip: ip,
items: items,
subtotal: Integer(subtotal),
shipping: Integer(shipping),
handling: Integer(handling),
tax: Integer(tax),
currency: currency,
return_url: return_url,
cancel_return_url: cancel_return_url,
allow_guest_checkout: allow_guest_checkout
}
amount = subtotal.to_i + shipping.to_i + handling.to_i + tax.to_i
puts "amount: " + amount.to_s
puts "items: " + items.to_s
response = gateway.setup_authorization(amount, setup_hash)
if !(response.success?)
puts response.message.to_s
end
And here's what I get in console:
amount: 10000
items: [{"name"=>"sample", "description"=>"desc", "quantity"=>1, "amount"=>10000}]
The totals of the cart item amounts do not match order amounts.
So, how come 10000 in amount doesn't match 10000 in items?
After long and boring debugging I found out the following:
internal hash items should be [:{name=>"sample", :description=>"desc", :quantity=>1, :amount=>10000}] rather than [{"name"=>"sample", "description"=>"desc", "quantity"=>1, "amount"=>10000}] in example above.
So, I've changed JSON parser to nice_hash and with items = ARGV[12].json it works like a charm.

Avoid building identical pages with Middleman proxy

I am creating a static website with Middleman, referencing products parsed from a spreadsheet.
My table has these columns:
_________________________________
| Product Name | Price | Category |
| Pet Food | $12 | Pets |
| iPhone | $500 | Phone |
| Pet toy | $25 | Pets |
|______________|_______|__________|
I created pages that show all products in the Pets and Phone categories using a template called product_category.html. It creates a page for each unique category, eg. pets.html and phone.html.
The problem is that given the way I proceed, Middleman builds one category page for each product in the category, and then skips it as it is identical:
remote: create build/pets.html
remote: identical build/pets.html
remote: create build/iphone.html
Here is my sample for config.rb:
rows_by_categories = app.data.spreadsheet.sheet1.group_by { |row| row.category }
#Category Landings
app.data.spreadsheet.sheet1.each do |f|
proxy "/#{f.category.to_s.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')}.html", "/product_category.html", locals: {
f: {
categorytitle: f.category,
name: f.name,
all_in_category: rows_by_categories[f.category],
price: f.selling_price,
},
categories: rows_by_categories.keys,
}, ignore: true
end
I understand the loop iterates on each line of my spreadsheet and recreates a page for the corresponding category. Yet the few tries I gave, eg. modifying app.data.spreadsheet.sheet1.each do |f| into rows_by_categories.each do |f| are unsuccessful. Any clue?
As mentioned I have no experience with middleman but I am going to try and help anyway.
It appears that your main issue is that you are looping the individual items rather than the groups. Please try the below instead.
rows_by_categories = app.data.spreadsheet.sheet1.group_by { |row| row.category }
#Category Landings
rows_by_categories.each do |category, rows|
path_name = "/#{category.to_s.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')}.html"
row_data = rows.map do |row|
{
categorytitle: row.category,
name: row.name,
price: row.selling_price,
}
end
proxy path_name, "/product_category.html", locals: {
products: row_data,
categories: rows_by_categories.keys
}, ignore: true
end
Here we loop through the categories and products will now be an Array of all the products in that category rather than a single product. This will, in my limited knowledge, create a single page for each category and give you a collection products that you can loop through

Insert into select with a hard coded value with Ruby Sequel

I am using Ruby Sequel to move data into reporting tables daily. I am consolidating data from three tables into a single table. I am doing this in Redshift so I have to use disable_insert_returning. The names of the columns in the two tables match each other but not the end table which means I am using graph and set_graph_aliases.
reports = db[:reports]
report_columns = [:user_id, :purchase_date, :sku]
spoons_select_graph = {
user_id: :users,
purchase_date: :spoon_receipts,
product_id: :spoon_receipts
}
spoons = db[:spoon_receipts]
spoons_select = spoons.graph(:users, user_id: :user_id).set_graph_aliases(spoons_select_graph)
forks_select_graph = {
user_id: :users,
purchase_date: :fork_receipts,
product_id: :fork_receipts
}
forks = db[:fork_receipts]
forks_select = forks.graph(:users, user_id: :user_id).set_graph_aliases(forks_select_graph)
reports.disable_insert_returning.insert(report_columns, spoons_select)
reports.where(channel: nil).update(channel: 'spoons')
reports.disable_insert_returning.insert(report_columns, forks_select)
reports.where(channel: nil).update(channel: 'forks')
The updates are taking forever. What I would like to do is add the channel to the insert select so that I don't have to go back and update.
You didn't provide an executable setup, so I haven't tested this, but I think it will work. The basic idea is to just add a constant column of results to your select.
reports = db[:reports]
report_columns = [:user_id, :purchase_date, :sku, :channel]
spoons_select_graph = {
user_id: :users,
purchase_date: :spoon_receipts,
product_id: :spoon_receipts,
channel: [:spoon_receipts, :channel, 'spoons']
}
spoons = db[:spoon_receipts]
spoons_select = spoons.graph(:users, user_id: :user_id).set_graph_aliases(spoons_select_graph)
reports.disable_insert_returning.insert(report_columns, spoons_select)
See the documentation for set_graph_aliases for more information.

each_with_index duplicated records

first of all, excuse me if my english is not always very good.
I have a problem with the each_with_index... When I show a project, I display the offers for this project. I have to make appear a banner in third position of offers, so I use each_with_index for that.
The problem is that: if I have sixteen offers on a project, I have sixteen times the entirely list of offers, and I don't know why.
This is my code:
- #offers.each_with_index do |offer, index|
- if !user_signed_in? && (#project.published? || #project.pending_attribution?)
- if #offers.size >= 3 && index == 3
= render 'offer_cta'
- contact_bloc = user_contact_bloc(offer.user, viewer: current_user, display_contact_info: offer.display_contact_info?)
.card.offer-card.mt-4[offer]{ class: offer_class(offer) }
.card-header.d-flex.justify-content-between.align-items-center.border-bottom-0
.offer-date
%span.text-muted
Offer filed on
= l(offer.created_at, format: '%d/%m/%Y à %Hh%M')
- if user_signed_in? && current_user.admin? && !offer.user.suspended_at.nil?
... exctera
Also, if I try this in my console, I don't have any problem...
Have you ever had this problem?
Don't hesitate to ask me if you need more information.
EDIT :
This is the code in my render 'offer_cta', a very basic code:
.row
.col-md-12
.project-cta.p-5.mb-2.text-center
%h5
%strong
blabla
%p.text-muted blablabla
%p.m-0
= link_to "Send a quote", "", class: "btn btn-primary px-3", data: { toggle: 'modal', target: '#modal-sign' }, onclick: "ga('send', 'event', 'button', 'Clic', 'Sign up');", tabindex: -1
#offers initialization:
#offers = #project.offers_accessible_by(current_ability, current_user)
#offers.mark_as_read_for(current_user)
#offers = #offers.includes(:offer_interactions, user: [:user_profile, :user_subscription, :user_contact])

Ruby Dashing: Issues with Jobs (on a CentOS Server)

I'm having a weird event occur, where my dashing dashboard's list widget is showing erroneous data. Here's the screenshot from my live Dashing widget
Erroneous Widget
Expected Output
What follows is the code for the widget:
Code in .erb
<li data-row="1" data-col="1" data-sizex="2" data-sizey="6">
<div data-id="facebook_insights" data-view="List" data-unordered="true" data-title="Facebook Insights: Weekly Post Views" data-moreinfo="Updated every 10 seconds"</div>
</li>
Code in job .rb
require 'mysql2'
social_count = Hash.new({ value: 0 })
time = Time.new()
date_time1 = Time.new(time.year, time.month, time.day-1)
...
SCHEDULER.every '10s' do
begin
db = Mysql.new(<HOST>,<USER>,<PASS>,<DBNAME>)
mysql1 = "SELECT <VAR> FROM <TABLE> WHERE <VAR> = '#{date_time1}' ORDER BY <VAR> DESC LIMIT 1"
...
result1 = db.query(mysql1)
...
rescue
ensure
db.close
end
result1.each do |row|
strrow1 = row[0]
$value1 = strrow1.to_i
end
...
social_count[0] = {label: "1:", value: $value1}
...
send_event('facebook_insights', { items: social_count.values })
end
What is really baffling, is that this code works for a similar widget using different data in the SQL query. Can anyone help me understand why?
I checked and re-checked the data and in my other, working code, I had my $value variables defined as $valueX with X being the number. I thought to myself "Maybe the variable names are getting confused due to them having the same name", so I changed my code to
Working Code
result1.each do |row|
strrow1 = row[0]
$variable1 = strrow1.to_i
end
...
social_count[0] = {label: "1:", value: $variable1}
Et Voila! Eureka! It worked. Not sure why it still got confused with the names, but from now on, my names will be unique!

Resources