how to search the data based on month in rails - ruby-on-rails

I need to search the data based on
By Month
By quater
By Half Year
By year in my revenue report.
Can any one help me how to do? My revenue report displays the booking information
in controller.
def revenue
#bookings = Booking.all
#bookings = Booking.where(currency: params[:currency]) if param[:currency].present?
end
My revenue.html.erb
<div class="col-md-3 col-lg-2 col-xlg-1">
<%= form_tag revenue_path, :method => 'get' do %>
<%= select_tag(:currency, options_for_select(["USD", "EUR"]), onchange: 'this.form.submit();', class: "form-control custom-select" , include_blank: true ) %>
<% end %>
</div>
<div class="col-md-6 col-lg-2 col-xlg-2">
<%= select_month(Date.today)%>
</div>
<div class="col-md-6 col-lg-2 col-xlg-2">
<select class="custom-select" data-style="form-control btn-secondary">
<option>By quater</option>
</select>
</div>
<div class="col-md-6 col-lg-2 col-xlg-2">
<select class="custom-select" data-style="form-control btn-secondary">
<option>By Halfyear</option>
</select>
</div>
<div class="col-md-6 col-lg-2 col-xlg-2">
<select class="custom-select" data-style="form-control btn-secondary">
<option>By Year</option>
</select>
</div>
<div class="table-responsive">
<table id="demo-foo-addrow" class="table m-t-30 table-hover no-wrap contact-list no-paging footable-loaded footable" data-page-size="2">
<thead>
<tr>
<th class="footable-sortable">#<span class="footable-sort-indicator"></span></th>
<th class="footable-sortable">
<input type="checkbox" class="check" id="minimal-checkbox-1">
</th>
<th class="footable-sortable">First Name<span class="footable-sort-indicator"></span></th>
<th class="footable-sortable">Last Name<span class="footable-sort-indicator"></span></th>
<th class="footable-sortable">Email<span class="footable-sort-indicator"></span></th>
<th class="footable-sortable">Status<span class="footable-sort-indicator"></span></th>
</tr>
</thead>
<tbody>
<% #bookings.each do |booking| %>
<tr>
<td><%= booking.id %></td>
<td><%= check_box_tag "bookings[booking_ids][]", booking.id %></td>
<td><%= booking.first_name %></td>
<td><%= booking.last_name %></td>
<td><%= booking.email %></td>
<td><%= booking.status %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
Can anyone help me out. Thanks in advance!

Based on debugging together with OP, found that the given method actually does much more than written. In last line of method, #booking = Booking.paginate(:per_page => 100) was being done which dissolved the query set generated earlier.
The final variant looked something like this:
def revenue
#bookings = Booking.all
if params[:currency].present?
#bookings = #bookings.where(currency: params[:currency])
end
if params[:search].present?
#bookings = #bookings.where('first_name like ?', "%#{params[:search]}}")
end
if params['date'] && params['date']['month']
#selected_date = Date.parse('2018-'+ params['date']['month'].to_s + '-01')
#bookings = #bookings.where('extract(month from created_at) = ?', params['date']['month'])
end
if params['date'] && params['date']['year']
#selected_year = params['date'] && params['date']['year'] ? Date.parse( params['date']['year'].to_s + '-01') : Date.today
#bookings = Booking.where('extract(year from created_at) = ?', params['date']['year'])
end
#per_page = params[:per_page] || Booking.per_page || 20
#bookings = #bookings.paginate(:per_page => #per_page, :page => params[:page])
end

Related

Searching with mongoid search

i have a 3 models user ,user_role and store .user_role belongs to user and store.i have a member controller where i have a search box one for stores,one for user_role(both are select box)below is method for search in user_role model
def self.filter(params)
filter = params[:search] || {}
user_roles = order(role_type: :asc)
if filter[:name].present?
end
if filter[:email].present?
end
if filter[:role].present?
user_roles = user_roles.where(role_type: filter[:role])
end
if filter[:store_id].present?
user_roles = user_roles.where(store_id: filter[:store_id])
end
user_roles
end
This is my member controller below
def index
if current_user.super_admin?
#total_user_roles = UserRole.filter(params).order(sort_column + " "
+ sort_direction)
#user_roles = #total_user_roles.page(params[:page]).per(10)
else
stores = current_user.user_roles.where(:role_type.in =>
['store_manager', 'merchant']).pluck(:store_id)
#total_user_roles = UserRole.where(:store_id.in =>
stores).filter(params).order(sort_column + " " + sort_direction)
#user_roles = #total_user_roles.page(params[:page]).per(10)
end
respond_to do |format|
format.html
format.csv { send_data #users.to_csv }
end
end
def sort_column
User.attribute_names.include?(params[:sort]) ? params[:sort] :
"first_name"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] :
"asc"
end
My search form in view.Should i change anything here in name and email field or just leave it as itis?
<section>
<div class="col-md-12">
<div class="row">
<section class="content-header">
<div class="white card-2">
<h3>Users</h3>
</div>
</section>
</div>
<div class="form white">
<!-- form start -->
<form class="form-horizontal">
<div class="box-body">
<h3>Search Users </h3>
<div class="col-md-3">
<label class="control-label"
for="order_search_order_number">First Name</label>
<input class="form-control" id="order_search_order_number"
name="search[name]" type="text" value="">
<span class="md-input-bar"></span>
</div>
<div class="col-md-3">
<label class="control-label">Email</label>
<input class="form-control" id="order_search_order_number"
name="search[email]" type="text">
<span class="md-input-bar"></span>
</div>
<div class="col-md-3 input">
<label class="control-label">Roles</label>
<%= select_tag("search[role]",
options_for_select(User::ADMIN_ROLES.map { |e| [e.humanize, e]},
params[:search] && params[:search][:role]), include_blank: true,
class: 'select2') %>
<span class="md-input-bar"></span>
</div>
<div class="col-md-3">
<label class="control-label">Store</label>
<%= select_tag("search[store_id]",
options_for_select(Store.all.order('name ASC').map { |e|
[e.name, e.id] }, params[:search] && params[:search][:store_id]),
include_blank: true, class: 'select2') %>
<span class="md-input-bar"></span>
</div>
</div>
<h4 class="pull-right"> Total Users: <b> <%=
#total_user_roles.count %></b>
</h4>
<br><br>
<div class="box-footer">
<button type="submit" class="btn btn-info">Filter</button>
Clear
</div>
<!-- /.box-footer -->
</form>
</div>
</div>
</section>
<div class="col-md-12">
<section class="section-data">
<!-- Section Data Table -->
<div class="row">
<div class="col-md-12">
<div class="box">
<div class="box-body table-responsive custom-sort">
<table class="table table-striped table-bordered nowrap data-
table" cellspacing="0" width="100%">
<thead>
<tr>
<th><%= sortable 'first_name', "Name" %></th>
<th><%= sortable 'email' %></th>
<th>Store</th>
<th><%= sortable 'role' %></th>
<th>Status</th>
<th>Action</th>
<th>Active?</th>
</tr>
</thead>
<tbody>
<% #user_roles.each do |role| %>
<tr>
<td><%= role.user.name%></td>
<td><%= role.user.email %></td>
<td><%= role.store.try(:name) %></td>
<td><%= role.role_type.try(:humanize) %></td>
<td><%= role.user.pending_invitation? ? "Pending" :
"Accepted" %></td>
<td>
<a class="ion-edit actions" href="/admin/members/<%=
role.id %>/edit"></a>
<% if role.user != current_user %>
<a class="ion-android-delete actions red" data-
sweet-alert-confirm="<%= t(:delete_confirm) %>" data-
method="delete" href="/admin/members/<%= role.user.id %>"></a>
<% end %>
</td>
<td>
<div class="onoffswitch">
<input type="checkbox" name="active" <%= "checked" if
role.user.active %> class="onoffswitch-checkbox" id="<%=
role.user.id %>" data-id="<%= role.user.id %>">
<label class="onoffswitch-label" for="
<%=role.user.id %>">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
</tr>
<% end %>
</tbody>
</table>
<div class="pull-right">
<%= paginate #user_roles, :theme => 'twitter-bootstrap-3' %>
</div>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
</div>
</section>
</div>
now i want to implement search for user name and email ,i tried lots of ways but cant come up with solution.as Iam not expert need some solution from a expert .iam using mongoid search gem.

need to save the checkbox value to other table in rails4 using fields_for.

this is the view page.---here using fields for method i'm passing data to JobPosting controller from check_box, Check box data need to be save in JobAssignment table.
<%- if #leads.present? -%>
<%= f.fields_for :assign do |x| %>
<%- #leads.each do |client| -%>
<% #requiter=UserRecruiter.where("reporting_to = ?", client.id)%>
<div class="col-md-3">
<table class="table table-bordered table-striped ">
<thead >
<tr class="bg-primary">
<th>
<%= x.check_box :assign_to, {}, client.id %>
<%= "#{client.first_name} #{client.last_name}" %>
</th>
</tr>
</thead>
<tbody style="height:110px">
<%- if #requiter.present? -%>
<%- #requiter.each do |req| -%>
<tr>
<td>
<%= "#{req.first_name} #{req.last_name}" %>
</td>
</tr>
<%end%>
<%end%>
</tbody>
</table> </div>
<%end%><%end%>
<%end%>
in controller---
def params_value
params.require(:job_posting).permit(:title, :description,:assign [])
end
def add_or_update_users
if params[:job_posting][:assign].present? && params[:job_posting] [:assign][:assign_to].present?
assign = params[:job_posting][:assign][:assign_to]
assign.each do |x|
#job.job_assignment = JobAssignment.where({ job_posting_id: #job.id, user_id: x }).first_or_create
#job.save
end
end
end
data is not saving to JobAssignment table.can any one tell me where i am doing wrong???
You can use build
def new
#jobpos = JobPosting.new
#jobpos.jobassignment.build
end
and replace the :assign to :jobassignment

Table in partial view not rendering

I am trying to display a table of jobs for a client sectioned by month year. The partial views evaluate and using binding.pry on each I can see that the correct values are there. The _month_section.html.erb date does not show, nor does the table in _jobs.html.erb
clients_controller.rb
def show
if id = params[:id]
if #client = current_user.clients.find_by_id(id.to_i)
if #client.jobs.count > 0
#jobs_by_month = #client.jobs.group_by{|j| j.created_at.strftime('%B %Y')}
end
else
redirect_to dashboard_path
end
end
end
show.html.erb
<% if #client.jobs.count > 0 %>
<div class="row">
<div class="col-lg-12 col-md-12">
<div class="panel panel-white">
<div class="panel-heading">
<h4 class="panel-title">Jobs</h4>
<div class="panel-body">
<%= render 'month_section' %>
</div>
</div>
</div>
</div>
</div>
<% end -%>
_month_section.html.erb
<div class="month_section" >
<% #jobs_by_month.each do |date, jobs| %>
<div>
<h3><%= date %></h3>
<%= render partial: 'jobs', :locals => {:jobs => jobs } %>
</div>
<% end %>
</div>
_jobs.html.erb
<div class="jobs">
<div class="row">
<div class="col-lg-12">
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Date</th>
<th>Start Time</th>
<th>End Time</th>
</tr>
</thead>
<tbody>
<% jobs.each do |job| %>
<tr>
<td><%= job.date%></td>
<td><%= job.start_time %></td>
<td><%= job.end_time %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
</div>
Turns out issue was due to a rogue non-terminated div tag on show.html.erb outside of the snippet I posted.

Set Rails date format for view

What's the most efficient way to display a date in a view in dd/mm/yyyy format?
I modified config/initializers/date_time.rb as such:
# Date
Date::DATE_FORMATS[:default] = "%d/%m/%Y"
However, I have a dropdown list that contains dates, and I use that to pass a date parameter date_id for searching purpose. If I use the above line, it doesn't return any results when it should.
My code:
balances_controller.rb
def statement
#dates = Balance.select("distinct(date)").order('date desc')
if (params[:date_id].blank?)
#latestDate = Balance.order('date desc').first.date
else
#latestDate = params[:date_id]
end
#summaryBalances = Balance.joins(:account).order('accounts.credit').where('date = :abc', {abc: #latestDate})
end
statement.html.erb
<h1>Statement at <%= #latestDate %></h1>
<%= form_tag("/statement", method: "get") do %>
<%= select_tag "date_id", options_for_select( #dates.collect {|d| [ d.date, d.date ] }) %>
<%= submit_tag("Search") %>
<% end %>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Account</th>
<th>Balance</th>
<th colspan="1"></th>
</tr>
</thead>
<tbody>
<% #summaryBalances.each do |balance| %>
<tr>
<td><%= balance.account.name %></td>
<td class='account-balance'><%= number_to_currency(balance.balance, unit: "£") %></td>
<td><%= link_to 'Edit', edit_balance_path(balance) %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
UPDATE:
html output:
<form action="/statement" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="✓" />
<select name="date_id" id="date_id"><option value="2015-11-30">30/11/2015</option>
<option value="2015-10-31">31/10/2015</option>
<option value="2015-09-30">30/09/2015</option></select>
<input type="submit" name="commit" value="Search" />
</form>
I'm not sure if this is the most efficient way of resolving this but I've managed to do what I need it to do by creating two separate helper methods. One to display the date in dd/mm/yyyy format and one to return the date in yyyy-mm-dd format for the parameter.
def date_format(date)
date.to_date.strftime("%d/%m/%Y")
end
def date_format2(date)
date.to_date.strftime("%Y-%m-%d")
end
Updated statement.html.erb
....
<%= form_tag("/statement", method: "get") do %>
<%= select_tag "date_id", options_for_select( #dates.collect {|d| [ date_format(d.date), date_format2(d.date) ] }) %>
<%= submit_tag("Search") %>
<% end %>
....

Print HTML Page Twitter bootstrap

I have a show page /invoices/show that displays contents of my Invoice
<p id="notice"><%= notice %></p>
<div class="row">
<div class="span7">
<h2 style="text-align:center;"> CONSTRUCTION LTD </h2>
<h3 style="text-align:center;">OHIO</h3>
<address style="text-align:center;"> Plot 10<br/>
</address>
<h3 style="text-decoration:underline; text-align:center;"> UTILITY BILL </h3>
<h4 style="text-decoration:underline; text-align:center;"> TAX INVOICE </h4>
<table>
<td valign="top" align="left">
<div style="float:left; width:450px;">No: <%= #invoice.id %></div>
<div style="float:right"> Date: <%= #invoice.invoice_date %></div>
</td>
</table>
<P></P>
<P></P>
<div>To: <%= #invoice.customer.name%></div>
<p></p>
<table class="table table-bordered table-condensed">
<thead>
<tr class="success">
<th><label class="control-label">Description</label></th>
<th><label class="control-label">Rate</label></th>
<th><label class="control-label">Tax</label></th>
<th><label class="control-label">Amount</label></th>
</tr>
</thead>
<tbody>
<% #invoice.invoice_items.each do | item| %>
<tr class="controls">
<td><%= item.description %></td>
<td><%= item.rate %></td>
<td><%= item.tax_amount %></td>
<td><%= item.amount %></td>
</tr>
<tr>
<td colspan="3" align="left"><strong>TOTAL:</strong></td>
<td colspan="1"><%= item.amount %></td>
</tr>
<% end %>
</tbody>
</table>
<div class="row">
<div class="span3">
<table>
<tbody>
<tr>
<td> <b>For Landlord</b></td></tr>
<tr> <td>Approved by:</td></tr>
<tr> <td>Sign:</td></tr>
</tbody>
</table>
</div>
<div class="span3" style="position: relative; align:left; left:150px;">
<table>
<tbody>
<tr>
<td> <b>For Tenant</b></td></tr>
<tr> <td>Approved by:</td></tr>
<tr> <td>Sign:</td></tr>
</tbody>
</table>
</div>
</div>
<br/>
<br />
<div>
<small><strong>Terms and Conditions</strong></small>
<table>
<tbody>
<tr>
<td><%= Settings.invoice_terms%></td>
</tr>
</tbody>
</table>
</div>
<br />
<div class="form actions">
<p>
<%= link_to t('.edit', :default => t("helpers.links.edit")),
edit_invoice_path, :class => 'btn btn-primary' %>
</p>
</div>
</div>
</div>
In my application.html.erb, I have this for CSS
<%= stylesheet_link_tag "application", :media => "all" %>
More to that, my application file has a nav-bar element.
I am trying to print the Invoices/show.html.erb page by going to the print option in a browser, however, I do not want it to include the nav-bar element in my application.html.erb file and the edit button in invoices/show.html.
I am using Twitter bootstrap, how can i go about this?
Here's one solution I've thought of:
What you can do is, in your show view (or the application layout) add a predicate:
if params[:controller] == "invoice" && params[:action] == "show"
# do or show whatever you want
end
Or you could do add a different layout to your views/layouts folder. Then in your controllers/invoice_controller
def show
# ...
render layout: 'a_different_layout_for_invoices'
end

Resources