Posts

using yield and content for in a partial

Time comes in your development process when you want one partial for two action with some changes in a section of html. In that case usually developers prefer to opt for if else or creating another partial or the whole new html template but all of them are good but not being rubyist style. To achieve that you can use yield and content_for for example : suppose you do have a partial _a.html.erb with some html in it <div class='main'> <p> this is main section</p> <div class = 'subpart'> <p> this is subpart </p> </div> <div class="block_of_code"> <%= yield :block_of_code %> </div> </div> In this block of code div you want to replace it with your block of code using same partial but with different block of code . So here I have used yield :block of code and now you can use content for rendering different block of code for different actions. for example you do have two action...

define_method in ruby

So one more step close to meta programming define_method. The usual way of defining new methods is to put your intended logic between def & end . And that works perfectly fine for the most part. But consider a situation where you have to create a series of methods all of which have the same basic structure save for one string, and which can only be one of a certain set of strings. Now, you might say we'll just define one method and accept that one string as a parameter, which we can then use wherever we want. And you'd be right. But, the problem with such an approach is it's not particularly declarative: you don't know exactly what values that optional parameter can accept.   class DefineMethod      define_method("perform_chunk") do |argument|          "performing chunk on #{argument}"      end      define_method("perform_chomp") do |argument|   ...

Delete Vs. Destroy In Rails

I’ve been writing this web app that has the following models: Order, Recipient, Message. An order has many recipients and a recipient has many messages. The recipients are also dependent upon the order and the messages are dependent upon the recipient. In Ruby code this looks like: Order.rb class Order < ActiveRecord::Base has_many :recipients, :dependent => :destroy has_many :messages, :through => :recipients end Recipient.rb class Recipient < ActiveRecord::Base belongs_to :order has_many :messages, :dependent => :destroy end The idea here is that when I delete an order, I also delete any associated recipients and any associated messages. My controller looked like this: def delete Order.delete(params[:id]) end There are a couple things wrong with this. The first and most important thing is that when I delete a row in the order table, it leaves orphaned rows in the order and messages table. I want to delete these rows as well...

print page while inserting new html before print with print.css in rails

Printing page is normal requirement with specified css and layout or some html which is not the current page but the problem comes when you want both the application css and print css to work for that layout. For that you need to do a trick you need to  preload the html in DOM and made it hide then on click of a print button hide the content which you don't want to come on the print page and show your preloaded html then print page comes with both the application/css and print.css and then again hide the print html and show the current html.

Integrating sunspot solr in rails with tomcat7

Image
At a first glance it seems too easy to integrate a gem in rails but when it comes to Tomcat7 and java dependencies it always end up in  tricky part so here it is follow these simple steps to integrate solr with all it's dependencies. first you need to have java environment sudo apt-get -y install openjdk-7-jdk sudo mkdir /usr/java sudo ln -s /usr/lib/jvm/java-7-openjdk-amd64 /usr/java/default Install tomcat sudo apt - get install tomcat7 sudo apt - get install tomcat7 - admin Then download solr (check for a newer version if you want) sudo wget http : //archive.apache.org/dist/lucene/solr/4.8.1/solr-4.8.1.tgz sudo tar - xvf solr - 4.8 . 1.tgz sudo cp - R solr - 4.8 . 1 / example / / opt / solr Set Solr with Tomcat: sudo cp - R / opt / solr / lib / ext /* /usr/share/tomcat7/lib/ sudo cp /opt/solr/webapps/solr.war /var/lib/tomcat7/webapps/solr.war sudo cp -R /opt/solr /var/lib/tomcat7 sudo chown -R tomcat7:tomcat7 /var/lib/tomcat7/solr service tomcat7 restart ...

Removing blank nodes recursively from nokogiri object

While parsing xml using Nokogiri it contains blank nodes in parent and childre nodes it looks like this #<Nokogiri::XML::Text:0x2d02be8 "\n"> and noblanks removes only blank nodes in parent node which become really hard when  recursively fetching nodes. To remove blank nodes recursively from every children and their children this code will work for you.       module NokoHacks       extend ActiveSupport::Concern       Nokogiri::XML::NodeSet.send :include, NokoHacks       Nokogiri::XML::Text.send :include, NokoHacks       Nokogiri::XML::Element.send :include, NokoHacks       def remove_blank_node         self.children.each do |node|           if node.blank?             node.remove    ...

Controller Concerns in Rails 4

If you setup a Rails 4 app, you’ll notice the app/models/concerns and app/controllers/concerns directories.  Concerns are modules that can be mixed into your models and controllers to share code between them.   Some developers falsely classify mixins as composition when they are actually a form of inheritance.  When you include a module in a class, that module’s methods are added to the inheritance chain just like a parent class’ methods are added to a subclass.  So, don’t think you’ve solved the problem of inheritance by simply splitting your inherited code into separate files! That being said, mixins can be a valuable tool to share code between classes that are otherwise unrelated. Here’s an example of how I chose to use it recently.    I have an admin control...