HighCharts pie chart in Rails - ruby-on-rails-3

I'm trying to display the January sales of products.
Here is the code in my products_controller.
#hi = LazyHighCharts::HighChart.new('graph') do |f|
f.options[:chart][:defaultSeriesType] = "pie"
f.options[:title][:text] = "Jan Sales"
#products.each do |p|
f.series(:name=>"Jan Sales", :data=>[:name=> p.title, :y=>p.jan])
end
end
I get this output:
http://i.imgur.com/eVF7n.png
It displays all of the name of the products (p.title) in the chart legend, however it only displays 1 value of the product's jan sales (p.jan).
Im using lazy high charts gem https://github.com/michelson/lazy_high_charts

Related

Get users of search products search results

I would know the best way to get all users who have products of the search result
My controller
#users = User.all // i must change here ?
if params[:product_type].present? && params[:location].present? && params[:discount].present?
#products = Product.near(params[:location], 1, units: :km).where(product_type: params[:product_type], discount: params[:discount])
end
My view
<% #products.each do |p| %>
<%= p.user.company_name %>
<% end %>
My method work but if a company have 2 products its display it 2 times
Assuming the following:
User has_many :products
Product belongs_to :user
You can do:
#users_from_product_results = User.where(id: #products.pluck(:user_id))
If several products refer to the same user, this User will be listed only once.
Rather than making 2 separate AR calls, use includes(eager_loading) which gives both products and users in a single query.
if params[:product_type].present? && params[:location].present? && params[:discount].present?
#products = Product.near(params[:location], 1, units: :km).where(product_type: params[:product_type], discount: params[:discount])
.includes(:user)
end
And in the view you could use #products.map(:users).uniq which gives all the users associated with the products.

Simple Form Association with Text Field

I have a Rails app that is using the simple_form gem. I have two models that are related, trades and stocks. In the form for trades, I want users to be able to enter their stock ticker symbol in a text field. Currently, I'm using the association function which renders a select box. The problem is that I want a text field instead since I have about a thousand stocks to choose from.
Is there a way I can do this (with or without Simple Form)?
the models:
class Trade < ActiveRecord::Base
belongs_to :stock
end
class Stock < ActiveRecord::Base
has_many :trades
end
the form on trades#new
<%= simple_form_for(#trade) do |f| %>
<%= f.association :stock %>
<% end %>
You should be able to just use this syntax:
<%= f.input :stock_id, :label => 'Enter your ticker:' %>
The problem here is that the user will not know what :stock_id is, as it's a reference to one of your many Stock objects.
So you probably want to implement a simple jquery autocomplete interface that returns a list of stocks like so:
[{:ticker => 'AAPL', :name => 'Apple Inc', :id => 1}, {:ticker => 'IBM', :name => 'International Business Machines', :id => 2}, etc ]
You can then display something like this as autocomplete results:
AAPL - Apple Inc
IBM - International Business Machines
and allow the user to select the one they are looking for. Behind the scenes you capture the :id and use that as your associated :stock_id.
You will need to add a stocks_controller action that takes a string and looks up Stocks based on a partial ticker and returns a max-number of stocks like 20.
def search
ticker_query = "%#{params[:ticker]}%"
stocks = Stock.where('ticker LIKE ?', ticker_query).limit(20)
render :json => stocks
end

Rails query question

I got a model called items, with a field called weeks. The content in weeks is as follows:
{2011=>["46", "47", "48", "49"]}
How can i do something like this:
Item.where(:week => week, :year => year)
When just passing one week example: 47 and year 2011
Thanks.
# Model
class Item < AR::Base
def self.with_week(weeek)
where("week LIKE (?)", "\"#{week}\"")
end
def self.with_year(year)
where("week LIKE (?)", "{#{year}=>")
end
end
usage
#items = Item.with_week(47).with_year(2011)

Month pagination with kaminari

I want to paginate posts by month so I added following scope in Post model
class Post
include Mongoid::Document
include Mongoid::Timestamps
scope :by_month, lambda {|end_date| Post.order_by(:created_at => :asc).where(:created_at.gte => (end_date.to_date.beginning_of_month), :created_at.lte => (end_date.to_date))}
end
In my controller I put
def show
#posts = Post.by_month(Time.now).page(params[:page]).per(20)
end
In view
<%= paginate #posts, :theme => 'month_theme' %>
<%= render #posts %>
Problems:
pagination is not working by month, I want to show all result of a month in a page, replacing params[:page] by params[:month]=2 or params[:month]=Feb
How do I view 'August 2011' instead of 1,2
Loop month and year like when you goto previous while in 'Jan 2011' it will goto 'Dec 2010'
I suppose this is not really a matter of pagination. Dealing with the params[:month] value for the query is something different from the page offset switching. You might not need a pagination library for that.
How about simply creating those links like this?
controller:
#posts = Post.by_month(Time.parse(params[:month]) || Time.now)
view:
<% Post.only(:created_at).map {|p| p.created_at.to_date.beginning_of_month}.uniq.sort.each do |m| -%>
<%= link_to_unless_current m, :month => m %>
<% end -%>
Of course you can combine this query with normal pagination if needed. But the pagination links should not be mixed with the month links in that case.

How do I use Capybara selectors to select a field properly

I've got a form which allows me to add/edit categories and sub categories within the one form. This form uses AJAX and to test it I've been using Capybara with some selectors.
The problem is with the selectors there seems to be subtle differences between when I'm creating a new category with sub categories to when I'm editing a category with sub categories.
Here is my create scenario:
#javascript #wip
Scenario: Create new category with sub categories
Given I am on the new category page
When I fill in "Name" with "Main" within the parent fields
And I follow "Add sub category"
And I fill in "Name" with "Sub1" within the 1st sub category fields
And I follow "Add sub category"
And I fill in "Name" with "Sub2" within the 2nd sub category fields
And I follow "Add sub category"
And I fill in "Name" with "Sub3" within the 3rd sub category fields
And I press "Save"
Then I should be on the "Main" category page
And I should see "Main"
And I should see "Sub1"
And I should see "Sub2"
And I should see "Sub3"
This works with selectors:
when /the parent fields/
"table tr:nth-child(1)"
when /the (\d+)(?:st|nd|rd|th) sub category fields/
pos = $1.to_i + 2
"table tr:nth-child(#{pos})"
On form:
= form_for #category do |f|
%table
%tr
%td= f.label :name
%td= f.text_field :name
%tr
%td(colspan=2)
%b Sub categories
- f.fields_for :children do |child|
= render "child_fields", :f => child
%tr
%td= link_to_add_fields "Add sub category", f, :children
%tr
%td= f.submit 'Save'
child_fields partial:
%tr.subs
%td= f.label :name
%td= f.text_field :name
When I use the same selectors though in my edit scenario I cannot select the 2nd category. Here is my edit category feature:
#javascript #wip
Scenario: Edit category with sub categories
Given a category exists
And category "Books" has sub category "Fiction"
And category "Books" has sub category "Non-Fiction"
And I am on the edit page for category "Books"
When I fill in "Name" with "Cars"
And I fill in "Name" with "Coupe" within the 1st sub category fields
And I fill in "Name" with "Sports" within the 2nd sub category fields
And I press "Save"
Then I should be on the "Cars" category page
And I should see "Cars"
And I should see "Coupe"
And I should see "Sports"
If I change my selector to:
when /the (\d+)(?:st|nd|rd|th) sub category fields/
pos = $1.to_i * 2 + 1
"table tr:nth-child(#{pos})"
Then it works for edit but not the new scenario.
Is there a way to use the same selector for both new & edit scenarios in my case?
Am I better using a different type of selector on my form? If so does anyone have any recommendations?
Use an id on the unique elements, and class combinations on repeated elements. With the right combination of class and id selectors you will always arrive at a unique child. Keep in mind you can group selectors on the one element.
So
Given a category exists
wait_for_xpath = '//element(#class = "categoryClass")'
And category "Books" has sub category "Fiction"
wait_for_xpath = "//element(contains (#class, 'categoryClass') and (#id, 'bookId'))//element(#id='fiction')"
etc