How to dry code? - ruby

I am working with has many through association in Application.
I access email from employee table in INDEX action of InventoryController like below code:
<% #inventories.each do |inventory| %>
<% inventory.employee_inventories.each do |e| %>
<%if e[:status]== 'ALLOTED'%>
<%= e.employee.email%>
<% end %>
<% end %>
<% end %>
Please help me How to DRY this code in view? Thanks in advance

#alloted_emails = #inventories.flat_map(&:employee_inventories).select do |i|
i[:status] == 'ALLOTED'
end.map do |i|
i.employee.email
end
Unless you want to create a scope as suggested by #Pavan, just introduce this var and use it.

If you are using the same code in different views then you can create a helper.
Otherwise write down this code like-
<% #inventories.each do |inventory| %>
<% inventory.employee_inventories.each do |e| %>
<%= e.employee.email if e[:status]== 'ALLOTED' %>
<% end %>
<% end %>
The main problem in your code is n+1 query problem. If you are using eager loading then there is no issue otherwise use eager loading using includes keyword in your query.

Related

Separating Ruby Code which requires a block with Partials

I want to use
<% form_tag ( <<variables from partial here>> ) do %>
on a partial; problem is on the IDE it already is trying to tell me I have invalid code; this is because the "DO" is to be paired with an "END". I cannot end on the partial itself; because there is some content after the "DO" which is on the page where the partial is called.
How do I use the form_tag in the partial while using the content I have from the page which called the partial?
Sounds like you're in a very sticky situation. This is what I think you've got from your description:
#partial_1
<% form_tag (:model_name) do %>
#partial_2
<% form_contents %>
<% end %>
#controller
#model = Model.load
render(partial1 << partial2)
You may have to correct me if I'm wrong, but is it possible instead to do this?
#controller
#model = Model.load
render(partial_1)
#partial_1
<% form_tag ... %>
<% render(partial_2) %>
<% end %>
#partial_2
<% all the form guts %>
If you're using straight ruby you're probably using the ERB library and you are binding every time, which should keep the variables live all the way through.

How to make SimpleForm boolean fields display as radio_buttons by default?

I'm using the Rails SimpleForm gem to create forms. I realize a boolean field named "accepts" (which typically displays as a checkbox) can be displayed as a pair of radio buttons instead, by changing
<%= f.input :accepts %>
to
<%= f.input :accepts, as: :radio_buttons %>
However, I am using a generic form template to dynamically generate a form for any Rails model in a loop like this:
<% some_rails_model.attribute_names.each |attr| %>
<% f.input attr %>
<% end %>
Since I don't know whether a particular attribute is a boolean (as opposed to a string, integer, etc.), I don't know when to add as: :radio_buttons.
Is there a way to make ALL booleans default to being displayed as radio buttons?
Alternatively, is there some simple way to check whether attr is a boolean, so that I then know when to add as: :radio_buttons?
I found a solution, which involves checking in the model whether a particular field is a boolean or not:
<% some_rails_model.attribute_names.each |attr| %>
<% if some_rails_model.columns_hash[attr].type == :boolean %>
<%= f.input attr, as: :radio_buttons %>
<% else %>
<%= f.input attr %>
<% end %>
<% end %>

Clarification on accessing methods

I am having trouble accessing a method i have written in a class, the problem is im trying to access it within an instance of that class, but would appreciate someone explain in a bit more details as to why this doesn't work and things to look at to get a solution.
I have created a simple helper method to use in my view to join two attributes
class TeamMember < ActiveRecord::Base
def fullname
"#{self.forename} #{self.surname}"
end
end
Within my view (show action) I want to be able to use this method
def show
#team_member = TeamMember.find(params[:id])
end
So doing this for example gives me an undefined method
<%= link_to fullname(#team_member) %>
OR
<% #team_member.each do |t| %>
<%= link_to fullname, t %>
<% end %>
Whereas this works
<% #team_member.each do |t| %>
<%= link_to "#{t.forename} #{t.surname}", t %>
<% end %>
Could someone help to clarify this for me so that i can learn from it please
You defined fullname method in your TeamMember class, but you try to call this method with implicit receiver in view, which is ActionView::Base instance. Instead, you should use explicit receiver, which must be TeamMember instance:
<%= link_to #team_member.fullname, #team_member %>
and:
<%= link_to t.fullname, t %>

ruby on rails 3.1 fragment caching

I have an app and I'd like to cache some parts of the page, and I have been reading a lot about the ways to do this.
I understand that fragment caching is the best way to do it on my project, but I canĀ“t find a simple example to learn how to implement this.
I would like to use the fragment cache with autoexpire.
<% cache(:action => 'recent', :action_suffix => 'all_products') do %>
All available products:
<% Product.all.each do |p| %>
<%= link_to p.name, product_url(p) %>
<% end %>
<% end %>
Where do I set the autoexpire? any examples around? how can I do this?
In your product model you can do something like this
after_save :expire_caches
after_destroy :expire_caches
# can't do this in a sweeper since there isn't a controller involved
def expire_caches
ActionController::Base.cache_store.delete_matched(%r{product\?for=\d+&fragment=products})

How can I use do edit-in-place on three different models from a View for a model those three belong_to?

I would like to enable edit-in-place functionality in a View that displays values from different models:
This is what I use currently and it does NOT work, but would like some alternatives:
I have a model called Campaign. In the controller, I do the following to list, in order, the three Models that belong_to a Campaign:
<% #campaign_events = campaign_events %>
<% #campaign_events.each do |campaign_event| %>
<% model_name = campaign_event.class.name.tableize.singularize %>
<p>
<%= link_to campaign_event.title, send("#{model_name}_path", campaign_event) %>
<span class='model_name'>(<%= model_name.capitalize %>)</span>
<%= campaign_event.days %> Days
</p>
<% end %>
campaign_event is a campaign_helper defined as:
module CampaignsHelper
def campaign_events
return (#campaign.calls + #campaign.emails + #campaign.letters).sort{|a,b| a.days <=> b.days}
end
end
I want to be able to click on the numerical value for Days when looking at the view/campaign/show and edit the value for :days (in this case, displayed as campaign_event.days
I'm not really sure about it, but I'll try to help... I believe something like the following may work for you:
# calls controller
in_place_edit_for :call, :days
# emails controller
in_place_edit_for :email, :days
# letters controller
in_place_edit_for :letter, :days
# campaign view
<% #campaign_events = campaign_events %>
<% #campaign_events.each do |campaign_event| %>
<% controller_name = campaign_event.class.name.tableize %>
<% model_name = controller_name.singularize %>
<p>
<%= link_to campaign_event.title,
send("#{model_name}_path", campaign_event) %>
<span class='model_name'>(<%= model_name.capitalize %>)</span>
<%= in_place_editor_field model_name, :days, {},
:url => {
:controller => controller_name,
:action => 'set_#{model_name}_title',
:id => campaign_event.id} %> Days
</p>
<% end %>
There's somethings I don't know exactly how to do:
1) in_place_editor_field model_name
I believe this won't work, but I don't know how to pass the model_name.
2) :action => 'set_#{controller_name}_title'
Not sure about it also. Just doesn't look right.
Anyway, hope it helps you... and forgive me if this is completely stupid.

Resources