Chartkick Datepicker/Range Selector - Possibility to choose dates to show data - ruby

I am using ahoy gem to track visits & chartkick gem to show the data to users.
In users dashboard page, I have around 3-4 charts and each of them show different data to user.
As of today, my charts only displays last 7 days & I do it by group_by_day(:started_at, last: 7).
= area_chart Visit.where(user: current_user).group_by_day(:started_at, last: 7).sum(:price), label: '$'
How can I add datepicker so users can select two dates (start & end date) and display data between selected/giving dates?
UPDATE:
I have created charts_controller.rb and get the charts from this controller & routes.
For instance:
class ChartsController < ApplicationController
def visits
result = Visit.where(user: current_user).group_by_day(:started_at, last: 7).count
render json: [{name: 'Visits', data: result}]
end
My routes.rb:
resources :charts, only: [] do
collection do
get 'visits'
end
end
My view:
= area_chart visits_charts_path

You can create a datepicker and add an onchange listenner on it, then you send the start and end date using ajax to a controller that renders your chart:
$(".end_at").change(function(e){
end_at = $(e.target).val();
start_at = $(".start_at").val();
$.ajax({
type: "POST",
url: "/charts",
data: {
start_at: start_at,
end_at: end_at,
},
success: function (data) {
$(".chart-container").html(data);
},
});
});
In your controller :
class ChartsController < ApplicationController
def visits
start_at = params[:start_at].to_date
end_at = params[:end_at].to_date
render :inline => "<%= area_chart Visit.where(user: current_user).group_by_day(:started_at, range: start_at..end_at).sum(:price), label: '$' %>"
end
end

Related

ruby, sinatra, Plivo API: render API call as HTML

I am making an API call to Plivo to list available telephone numbers.
I can access the returned response and print the desired elements in my terminal BUT I do not know how to render them as HTML on my web page. This is my problem.
In the terminal, the response to a successful call is:
{"api_id"=>"23f1f0f0-0808-11e3-a442-22000ac6194a",
"meta"=>
{"limit"=>1, "next"=>nil, "offset"=>0, "previous"=>nil, "total_count"=>1},
"objects"=>
[{"group_id"=>"23928520636825",
"number_type"=>"local",
"prefix"=>"646",
"region"=>"New York, UNITED STATES",
"rental_rate"=>"0.80000",
"resource_uri"=>
"/v1/Account/MAZDQ1ZJIYMDZKMMZKYM/AvailableNumberGroup/23928520636825/",
"setup_rate"=>"0.00000",
"sms_enabled"=>true,
"sms_rate"=>"0.00800",
"stock"=>50,
"voice_enabled"=>true,
"voice_rate"=>"0.00900"}]}
"0.00900"
New York, UNITED STATES
646
The Ajax script which generates the response is:
$(".localsearch").click(function() {
var country_iso = $("#local").val();
var region = $("#region").val();
var prefix = $("#prefix").val();
$.ajax({
type: "GET",
url: "/local/data",
data: { 'country_iso' : country_iso, 'region' : region, 'prefix' : prefix },
success: function(data) {
alert(data)
},
});
});
The alert doesn't help and just shows the entire page.
The ruby code is:
get '/local/data' do
country_iso = params[:country_iso]
region = params[:region]
prefix = params[:prefix]
p = RestAPI.new(AUTH_ID, AUTH_TOKEN)
params = {'country_iso' => country_iso, 'region' => region, 'prefix' => prefix, 'limit' => '1'}
response = p.get_number_group(params)
obj = response.last
pp response.last
#region = obj["objects"][0]["region"]
puts #region
#prefix = obj["objects"][0]["prefix"]
puts #prefix
erb :search
end
So, sorry it's long and to summarize, how do I extract elements from the API response and print them as HTML? Many thanks in advance.
In the view I have tried:
<%= #region %> and <%= obj['region'] %> and <%= obj['objects][0]['region'] %>and none of them work.
Yours is a perfect use case of of rendering a partial through a ajax call.
So what you can do is:
Make your Sinatra action return html using rails like render partial functionality like this
http://steve.dynedge.co.uk/2010/04/14/render-rails-style-partials-in-sinatra/
(to get rails like partial functionality in sinatra you can use this gem also https://rubygems.org/gems/sinatra-partial )
Now since now your sinatra action returns a valid html, in your ajax success function you can just write:
$(".localsearch").click(function() {
var country_iso = $("#local").val();
var region = $("#region").val();
var prefix = $("#prefix").val();
$.ajax({
type: "GET",
url: "/local/data",
data: { 'country_iso' : country_iso, 'region' : region, 'prefix' : prefix },
success: function(data) {
$('unique_identifier_of_your_partial_on_the_html_dom').html(response)
}
});
});
another example of rendering partial in sinatra:
Ruby w/ Sinatra: Could I have an example of a jQuery AJAX request?
extract out the html that you want to populate with the response from this ajax call into a a separate erb file lets say , _my_response_partial.html.erb
now suppose this is your search.html.erb file.
#something somrthing
<%= erb(:my_response_partial, locals => {:region => #region, :prefix => #prefix},:layout => false) %> #pass whatever data you want to pass to a partial using locales
# something something
and in your get action replace the last line with:
erb(:my_response_partial, locals => {:region => #region, :prefix => #prefix},:layout => false)
By this way your action will just return the html required to populate that partial.

X editable django and jquery yu

I'm trying to update a field using X Editable but I do not make it till the end.
Here is my code:
#views.py
def update_task(request, task_id):
if request.is_ajax():
task = request.GET.get('task')
updated_task = Task.objects.get(pk=task_id)
updated_task.task = task
updated_task.save()
return HttpResponse('true')
else:
return HttpResponse('not ajax')
#urls.py
url(r'^update_task/(?P<task_id>\d+)/$', 'todo.views.update_task', name='update_task'),
#html file
<script>
$(document).ready(function () {
$("#task").editable({
type: 'text',
pk: 144,
url: '/update_task/144/',
title: 'Enter task',
});
});
</script>
When I'm trying to update it console gives me:
link/update_task/144 404 (Not found)
I don't see that you have a url defined to capture the task_id
I think you need to either put the task_id in the parameter, so something like url + '?task_id=144.
If you do this, you need to change your javascript line to look like this:
url: '/update_task/' + '?task_id=144',
or, you need to leave it as is, and add a line to your url conf to capture the parameter:
url(r'^update_task/(?P<task_id>\d+)/$', 'todo.views.update_task', name='update_task'),
and then in your views:
def update_task(request, task_id=None):#if task_id is optional, set it to =None or something
if request.is_ajax():
do stuff here with task_id
also, you will need to take out the task_id = request.GET.get('task_id')
because task_id is not in the GET body.

i18n is changing the output of Ajax call

I have a datepicker function that was working pefectly well until I internationalized my application. But now, it's not working anymore. Here is my Ajax function :
$datepicker.change(function(){
currentDate = $datepicker.datepicker( "getDate" );
dateString = $.datepicker.formatDate("yy-mm-dd", currentDate);
console.log("My dateString is: "+dateString);
Here my console is showing the right date
$.ajax({
type: "POST",
url: "movements/getTO/",
data: {"date":dateString},
}).done(function(data) {
$("#resultTO").html(data[0])
$("#resultQty").html(data[1])
});
});
with this controller function :
def getTO
selected_date = Date.parse(params[:date])
new_html_to_return1 = Movement.where(:movement_date =>selected_date, :user => current_user.email).sum("turnover")
new_html_to_return2 = Movement.where(:movement_date =>selected_date, :user => current_user.email).sum("quantity")
#table = [new_html_to_return1, new_html_to_return2]
render :json => #table
end
My routes.rb :
post "movements/getTO"
scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
resources :movements
(...)
end
match '*path', to: redirect("/#{I18n.default_locale}/%{path}"), constraints: lambda { |req| !req.path.starts_with? "/#{I18n.default_locale}/" }
match '', to: redirect("/#{I18n.default_locale}")
At this point, my datepicker was having a route trouble. So I added in routes.rb
match 'movements/getTO', to: redirect("movements/getTO")
But that's not right because now I have as output : < and !. Incredible for sums isn't it ???

How to pass back using information ajax POST request

Currently I'm working on the slider, each slide is certain state using state machine. Now from one page I'm making post request with the data, which gets sent, if it passes validation, I would like to return "name of the state" to my view page, so I can move the slide ?
Is this better way of doing this ? The reason I chose slides as state so I can run query to figure out how many of my client made to certain slide.
So currently I'm making ajax post request.. (Ignore done for part)
$.post("/xxx/tracks", { name: "xx", time: "xx", pa: "xx" }).done(function(data) {
alert("Data Loaded:" + data);
});
So now in my controller
def tracks
binding.pry
# some condition determining val accordingly
respond_to do |format|
format.json { head :ok } #return val somehow
end
end
so my question is how do I return some value back ?
You can pass html, text or just the response header if you want to render nothing.
Html for example, just render the view you want:
def tracks
binding.pry
# some condition determining val accordingly
render 'tracks' #tracks.html.erb
end
$.post("/xxx/tracks", { name: "xx", time: "xx", pa: "xx" }, function(data) {
$("selector").append(data)
}).error(function(response, data){
alert('error processing the request')
})
For Text:
def tracks
binding.pry
# some condition determining val accordingly
if expression
render text: 'success', status: :ok
else
render text: 'Error processing your request', status :bad_request
end
end
$.post("/xxx/tracks", { name: "xx", time: "xx", pa: "xx" }, function(response, data) {
alert(data.responseText)
}).error(function(response, data){
alert(data.responseText)
})
To render just the headers
def tracks
binding.pry
# some condition determining val accordingly
if expression
head :created
else
head :unprocessable_entity
end
end
$.post("/xxx/tracks", { name: "xx", time: "xx", pa: "xx" }, function() {
alert('seccess')
}).error(function(){
alert('error')
})
you can render too json data.

ASP MVC3 and Telerik DatePicker control blocking year/month pick after Ajax action

So I'm in big trouble here.
<%: Html.Telerik().DatePicker().Name("dataWprowadzeniaOd").ShowButton(true)%> -
<%:Html.Telerik().DatePicker().Name("dataWprowadzeniaDo").ShowButton(true) %>
I have these two nice DatePickers, a list and some filtering options, all on Ajax. Now when Ajax action occurs (like filtering, refreshing table, changing pages) these two cute little things block in a way user is not able to change month and year there.
Here's beginning of table code:
Html.Telerik().Grid(Model)
.Name("Main")
.DataKeys(keys => keys.Add(p => p.DepozytID))
.Localizable("pl-PL")
.Pageable(paging => paging.Enabled(true).PageSize(20))
I've tried putting:
$('#dataWprowadzeniaOd').tDatePicker({ format: 'yyyy-MM-dd', minValue: new Date(1899, 11, 31),
maxValue: new Date(2100, 0, 1) });
$('#dataWprowadzeniaDo').tDatePicker({ format: 'yyyy-MM-dd', minValue: new Date(1899, 11, 31),
maxValue: new Date(2100, 0, 1) });
at the end of every callback/ajax function, but it removes month/year bar from calendar so I can't check if it works o.O
Tried this:
$('#dataWprowadzeniaOd').attr('disabled', 'disabled');
$('#dataWprowadzeniaOd').attr('disabled', '');
too, but no effect.
function filtruj() {
var newurl = '<%: Url.Content("~/RejestrDepozytow/ListaDepozytow") %>';
var filtr = {};
filtr.typStatusu = $("#typStatusu").val();
filtr.rodzajDepozytuID = $("#rodzajDepozytuID").val();
filtr.dataWprowadzeniaOd = $("#dataWprowadzeniaOd").val();
filtr.dataWprowadzeniaDo = $("#dataWprowadzeniaDo").val();
filtr.podmiotSkladajacyID = $("#podmiotSkladajacyID").val();
filtr.podmiot = $("#podmiot").val();
filtr.sygnaturaSprawy = $("#sygnaturaSprawy").val();
filtr.barcode = $("#barcode").val();
filtr.numerLp = $("#numerLp").val();
filtr.numerRok = $("#numerRok").val();
$.ajax({
type: "POST",
async: false,
url: newurl,
data: filtr,
success: function (dane) {
$("#gridDepozytow").html(dane);
}
});
}
Here's method used just before datepicker getting blocked.

Resources