Will_Paginate with Sunspot – Ruby on Rails

Hey all,

I’ve just gone through setting up will_paginate with sunspot 1.3, just thought I’d share in case anyone else has any troubles/improvements.

In my case I’m setting it up on a products model.

  #products.rb

  #Sunspot/Search
  searchable do
    text :title, :boost => 5
    text :description
  end

#products_controller.rb
  #Products search
  def search

    #Create vars
    search = Product.search do
      fulltext params[:search]
      paginate :page => params[:page] || 1, :per_page => 2
    end    

    #Retrieve results
    @products = search.results

    #Create responses
    respond_to do |format|
      format.html { render 'index' }
    end
  end

#Search partial - _search.html.erb
 'products', :action => 'search', :method => :get do %>
    '25', :class => 'search' %>

#Product display page - index.html.erb

    "products/product_line", :locals => {:product => product} %>

      
 false, :params => { :search_text => params[:search_text] } %>

Well that’s about it, if anyone needs any further info or has any tips, leave a comment and I’ll get back to you asap.

Uninstalling a Gem – Ruby on Rails

Due to a few recent issues with SunSpot (see my previous posts), I’ve decided to uninstall it until I have enough time to work on it properly. Once again, Ruby makes this pretty simple:


chris@chris-VirtualBox:~/site$ sudo gem uninstall sunspot
[sudo] password for chris: 

You have requested to uninstall the gem: sunspot-1.2.1 sunspot_rails-1.2.1 depends on [sunspot (= 1.2.1)]
If you remove this gems, one or more dependencies will not be met.
Continue with Uninstall? [Yn]  y


Remove executables: sunspot-solr, sunspot-installer in addition to the gem? [Yn]  y


Removing sunspot-solr
Removing sunspot-installer


Successfully uninstalled sunspot-1.2.1

Setting Default Slider Value – jQuery UI

Had a bit of a muck around with jQuery sliders today, just though I’d post the code I used to set a default value in case anyone else has a use for it:


//Create sliders
$(document).ready(function() {
   $(".slider").slider("option", "value", 100);
});

Just sub whatever you want your default slider value to be in place of the 100. The jQuery documentation is also pretty useful if you’re chasing any more info: jQuery Slider Documentation

Cheers

Copying Text from Notepad++ to Microsoft Word

In the middle of doing up some documentation I realised that the formatting and colors aren’t kept when you copy and paste from Notepad++ to Microsoft Word. Luckily Google was able to help out by showing me default plugin that lets you keep all the syntax highlighting etc.

1: Hightlight any of the text you want copied
2: From the menu select Plugins > NppExport > Copy all formats to clipboard
3: Paste into Microsoft Word
Highlight code
Highlight code
Select NppExport
Select NppExport
Paste in Word
Paste in Word

And that’s it – all done!

How to Edit a Message Catalog Definition – PeopleSoft

Editing a message catalog definition in PeopleSoft is pretty straight forward, simply browse to the following menu path:

PeopleTools > Utilities > Administration > Message Catalog

Enter your message set number into the prompt. Note that if you do not know your message set number you can use the following to find it:

SELECT *
FROM PSMSGCATDEFN
WHERE MESSAGE_SET_NBR = '12345'
      AND MESSAGE_NBR = '12345';
      
SELECT *
FROM PSMSGCATDEFN
WHERE MESSAGE_TEXT LIKE '%what your message is%';

Once you have your message simply edit the text and description appropriately then save!

Sleep Function Within PeopleCode – PeopleSoft

While at work today I discovered that there doesn’t appear to be any built in functionality to allow for a delay to be implemented within PeopleCode. Thankfully there are a couple of roundabout way you can go about doing this:

#1: Works if you are sitting on an Oracle DB

/* The following code creates a 5 second delay using DBMS_LOCK.SLEEP */
&s = 5;
SQLExec("exec DBMS_LOCK.SLEEP(:1)", &s);

#2: Note that this method is not as efficient as DMBS_LOCK.SLEEP()

/* The following code creates a 5 second delay using the java sleep method */
&s = 5;
GetJavaClass("java.lang.Thread").sleep(1000 * &s);

Truncating a String within a View – Ruby on Rails

Another quick post, just on how to truncate a string within a view. I used the following in order to ensure that my order_item title is never longer than 100 characters:

Title:  100, :seperator => ' ...') %>

 

This will output the title, if it’s longer than 100 characters the string will be shortened to 100 characters (minus the separator’s length). The neat thing about truncate is all the separator functionality is built in – you don’t have to throw any if/else logic in to check whether it’s required or not.

Output:

#Less than 100 characters
Title: lentesque porttitor

#More than 100 characters
Title: lentesque porttitor, velit vel ullamcorper pharetra, augue erat pharetra risus, quis bibendum fel…

Good Luck!

Adding a Link Within a Flash Notice/Message – Ruby on Rails

I came across an instance where I needed to add a link to flash notice tonight. Manually creating a hyperlink seems to work fine, however I was chasing a rubyish way of doing things:

flash[:notice] = "Order created - Click here to pay for it!"

 

Luckily a quick Google revealed the answer:

flash[:notice] = "Order created - Click here to pay for it!".html_safe

 

If you’re using rails 3 make sure you throw in the .html_safe or your link will be appear as plain text. Just a quick screenshot of the end result below:

Anyway, that’s all. Good Luck!

Passing Variables to a Partial from a View – Ruby on Rails

Just another one of those things that I find myself needing to do from time to time. Luckily this is all pretty straight forward (thankyou Ruby on Rails!). Assuming your partial is a form in the locations folder and that the variable you’re expecting is location all that you have to do is following:

View: users/show.html.erb

 'locations/form', :locals => { :location => @user.location } %>

Partial: locations/_form.html.erb


Street: 

Good luck!