How to make a Chartkick Multipleseries - ruby-on-rails

Hi I'm practically new on ruby on rails and having a hard time on making multipleseries on chartkick.
Based on :
http://chartkick.com/
Making Chartkick multipleseries is easy as :
<%= line_chart #goals.map{|goal|
{:name => goal.name, :data => goal.feats.group_by_week(:created_at).count }
} %>
but it seems to me is not working as it should since group_by deprecated in rails 4.
is there a way to produce the same effect like the code above?
thank you

Related

How to get multiple fields form into one Json column using ActiveAdmin Active Admin Ruby on Rails and MySQL?

Hi I'm new to ActiveAdmin, it's inherent Formtastic and Ruby (2.6) on Rails (6.1) and need to make a modification on an existing system.
To make it short, I have a Prices model, which is filled with data on a prices.html.erb form.
I need to add key/value pairs (eg: {"fee_1": 10, "fee_2": 20,...}) to be stored in a JSON "fees_list" column, in a MySQL database.
I'm having issues about how to modify the form to input the values.
Amongst many attempts so far, in the prices.html.erb Formtastic form I've tried (Edit: Thanks to Lam Phan for typo fix):
<%= f.inputs, for: => :fees_list do |j| %>
<%= j.input :fee_1, require: false, input_html: { value: resource.fees_list['fee_1'] } %>
<%= j.input :fee_2, require: false, input_html: { value: resource.fees_list['fee_2'] } %>
<%= end %>
Additional information is that I have a #prices reference available in the prices.html.erb file.
But the resulting page does not display the price inputs. Do I need some Gem to edit/show the JSON data in the DB?
Could someone help please? Thanks a lot to this great community in advance. Best regards!

How to properly use number_to_phone or similar way to style a phone number input

I always like to mention that I'm fairly new to rails and building my first app. So I apologize in advance if the following question is too stupid.
I have a form with a :office_number that I would like to format. I'm running rails 4 with bootstrap 3. Here's my current code:
<%= f.input number_to_phone(:office_number, :groupings => [2, 4, 4], :delimiter => "-"), label: "Office #", :input_html => { class: "form-control" }, :label_html => { :class => 'form-label' } %>
I get no erros, but when I type the number, it doesn't get styled as it was supposed to. I want to achieve a number like this 48-1234-5679. Better yet, if is not asking too much, I want the number to show exactly like this (48) 1234-5678. Any help on how can I achieve that?
Check the docs. Maybe you need the area_code option.
GL & HF.

Why is a data hash returning a compile error in Rails 3.1 and 3.2 app?

I am messing around with Railscast #102 Auto-Complete Association in a Rails 3.1 app, which I've just upgraded to 3.2.
When I try to add the data hash to a text field
<%= f.text_field :category_name, data: {autocomplete_source: Category.order(:name).map(&:name)} %>
Rails generates a compile error, indicating there is a problem with the data hash.
So I rewrote the hash to
<%= f.text_field :category_name, 'data-autocomplete_source' => Category.order(:name).map(&:name)} %>
This is working, but can someone explain to me if there are any differences between these two approaches that I should be aware off.
Secondly, if I set up the autocomplete function with a static hash of values
$(function() {
$('#post_category_name').autocomplete({
source: ['foo', 'food', 'four']
});
});
The autocomplete works. But if I use the data hash:
$(function() {
$('#post_category_name').autocomplete({
source: $('#post_category_name').data('autocomplete_source')
});
});
autocomplete is not working? In the console it returns
GET http://app.dev/post/4/foo%20food%20four?term=foo 404 (Not Found)
This is confusing me, as there are clearly related terms in the GET request. Is this due to my adjusting the data hash, or is something else going on here?
Thanks for your ideas or suggestions to help me learn how all this works.
The symbol: value JavaScript-ish Hash syntax is new in 1.9 so that explains your first problem. Switching to a 1.9 Ruby or using the traditional syntax:
<%= f.text_field :category_name, :data => { :autocomplete_source => Category.order(:name).map(&:name) } %>
should take care of that.
Check the generated HTML, I think the data attribute will be data-autocomplete-source rather than data-autocomplete_source so try this:
$('#post_category_name').autocomplete({
source: $('#post_category_name').data('autocomplete-source')
});

How would I check if a value is found in an array of values

I want to perform an if condition where, if linkedpub.LPU_ID is found in an array of values(#associated_linked_pub), do some action.
I tried the following but the syntax is not correct.
Any suggestion is most welcomed..Thanks a lot
<% for linkedpub in Linkedpub.find(:all) %>
<% if linkedpub.LPU_ID IN #associated_linked_pub %>
# do action
<%end%>
<%end%>
You can use Array#include?
So...
if #associated_linked_pub.include? linkedpub.LPU_ID
...
Edit:
If #associated_linked_pub is a list of ActiveRecord objects then try this instead:
if #associated_linked_pub.map{|a| a.id}.include? linkedpub.LPU_ID
...
Edit:
Looking at your question in more detail, it looks like what you are doing is VERY inefficient and unscalable. Instead you could do...
For Rails 3.0:
Linkedpub.where(:id => #associated_linked_pub)
For Rails 2.x:
LinkedPub.find(:all, :conditions => { :id => #associated_linked_pub })
Rails will automatically create a SQL IN query such as:
SELECT * FROM linkedpubs WHERE id IN (34, 6, 2, 67, 8)
linkedpub.LPU_ID.in?(#associated_linked_pub.collect(&:id))
Using in? in these cases has always felt more natural to me.
if #associated_linked_pub is an array, try
if #associated_linked_pub.include?(linkedpub.LPU_ID)
#associated_linked_pub.collect(&:id).include?(linkedpub.LPU_ID)

Can you recommend good data grid class/gem for Ruby on Rails?

Can you recommend good data grid class/gem for Ruby on Rails? Like http://code.google.com/p/zend-framework-datagrid/ for ZF
You can also try datagrid gem. That is focused not only grids with columns but also filters.
class SimpleReport
include Datagrid
scope do
User.includes(:group)
end
filter(:category, :enum, :select => ["first", "second"])
filter(:disabled, :eboolean)
filter(:confirmed, :boolean)
filter(:group_id, :integer, :multiple => true)
integer_range_filter(:logins_count, :integer)
filter(:group_name, :string, :header => "Group") do |value|
self.joins(:group).where(:groups => {:name => value})
end
column(:name)
column(:group, :order => "groups.name") do |user|
user.name
end
column(:active, :header => "Activated") do |user|
!user.disabled
end
end
Not sure if this is what you are looking for but checkout https://github.com/wice/wice_grid
If you are looking for a powerful client-side grid, supporting pagination, sorting, grouping, editing, export to Excel, PDF, etc, you can check Shield UI's Grid component.
Here's a tutorial on how to integrate it in Rails.
If you are looking for things like pagination, ordering, sorting etc, then rails does all this automatically.
So, for example if you wanted to sort all row by a particular column, then the title of that column could simply be a link that sorted the results by that column and then re rendered the grid.
So if you want to build a data grid that isn't AJAXy then this is pretty simple. If you are looking for a way to do it with XHR requests then you can use jQuery to make requests in the background.
As fas as a gem that does all this automatically, I couldn't find one, but I can't see why you couldn't do it yourself easily with the foundations that rails provides.

Resources