Thursday, December 27, 2012

Assign class or function to Dom element on the fly

Hello Guys,
        Want to assign the class to dom element on the fly? then simply use javascript function which listed below.

demo.html

Lets assume we have anchor tag on it's click we have to apply css class.

<a id="anchor" href="" target='_blank' class='active' onclick="javascript:open_url(this.id, 'google.com');"> Open this link in new window or tab </a>

// javascript  function
<script type='javascript'>
  function open_url(id, url){
   // verify dom has 'active' class or not
   if ($('#' + id).hasClass('active')) {
     if(url != ''){
    // it's open url in new window
     window.open(url);
   }
  }
}
</script>

Thursday, December 13, 2012

Preventing Recursive Method Calls in Salesforce

Hello Guys,
        Yesterday i was playing with salesforce custom object. I had task to update the object once particular field get updated /inserted (for same object). 

Eg. I have Student__c is custom object on salesforce.

Structure of Student__c object is like:
Id, Name__c, Score_in_maths__c, Score_in_science__c, Total_score__c

So when trying to update any score value required to update Total_score__c  accordingly.  To fulfill  this i have added trigger on Student__c (after update) but it results as recursive loop.

To overcome above issue just followed the steps mention in http://blog.jeffdouglas.com/2009/10/02/preventing-recursive-future-method-calls-in-salesforce/. And it works for me.

Free feel to leave comment or ask queries. :)

Thursday, October 4, 2012

Generate chart using axlsx gem

Hello Rubies,
        I have generated the graph via axlsx gem of ruby. Follow the easy steps to generate 3D stacked bar graph.

Install axlsx gem via
   gem install axlsx

Lets, generate graph.rb file and than run ruby graph.rb

require "rubygems"
require "axlsx"

 p = Axlsx::Package.new
 wb = p.workbook

 wb.styles do |s|
    wb.add_worksheet(:name => "Bar graph demo") do |sheet|
        sheet.add_row ["A Simple Bar Chart"]
        sheet.add_chart(Axlsx::Bar3DChart, :start_at => "A1", :end_at => "F27", :grouping => :stacked, :show_legend => false, :shape => :box, :barDir => :col) do |chart|
         chart.valAxis.title = "Volumes"
         chart.catAxis.title = "Periods"
         chart.add_series :data => [1,2,3], :labels => ['Mar', 'Apr','May'], :colors => ['92D050', '92D050', '92D050']
         chart.add_series :data => [4,2,6], :labels => ['Mar', 'Apr', 'May'], :colors => ['FFFF00', 'FFFF00','FFFF00']
       end
   end  
end

file = File.open('/home/Desktop/graph.xlsx', 'w')
p.serialize(file)

Lets invoke ruby graph.rb on console and get the stacked bar chart.

Thursday, September 20, 2012

Axlsx to support line break (\n)

Hello Rubies,
      Yesterday when i was playing with axlsx gem find something interesting. Requirement is need to allow line break in content. And current axlsx gem does not support that feature. So add this patch to your existing gem or either point directly to git repository b'coze that patch is unpublished as for now.

Add this line to axlsx1.2.3/lib/axlsx/workbook/worksheet/worksheet.rb

Remove line #500  

str.gsub(/[[:cntrl:]]/,'')

and Replace with

  if RUBY_VERSION == "1.8.7"
    nasty_control_char_matcher = Regexp.new("[\x01\x02\x03\x04\x05\x06\x07\x08\x1F\v\xE2]")
  else
    nasty_control_char_matcher = Regexp.new("[\x01\x02\x03\x04\x05\x06\x07\x08\x1F\v\u2028]")
  end

  str.gsub(nasty_control_char_matcher,'')

or

gem 'axlsx', '1.2.3', :git => 'git://github.com/randym/axlsx.git'

Hope this post is useful to you folks :).

Wednesday, September 5, 2012

jumping of browser window when using cursor keys

Hello Guys,
           There is wired problem when using auto completer on page and try to use arrow key for navigation.. it simply jump the browser window.

To over come this problem just add the patch in controls.js

replace line 212 to 214 with

    if(this.index > 0) {this.index--;}
    else {
      this.index = this.entryCount-1;
      this.update.scrollTop = this.update.scrollHeight;
    }
    selection = this.getEntry(this.index);
    selection_top = selection.offsetTop;
    if(selection_top < this.update.scrollTop){
    this.update.scrollTop = this.update.scrollTop-selection.offsetHeight;
    }

replace line 217 to 220 with

    if(this.index < this.entryCount-1) {this.index++;}
    else {
      this.index = 0;
      this.update.scrollTop = 0;
    }
    selection = this.getEntry(this.index);
    selection_bottom = selection.offsetTop+selection.offsetHeight;
    if(selection_bottom > this.update.scrollTop+this.update.offsetHeight){
      this.update.scrollTop = this.update.scrollTop+selection.offsetHeight;
    }
  
add line after 297
  this.update.scrollTop = 0;

In short by replacing two functions markPrevious() & markNext() will fix our problem.

Execute raw query and manual connection

Hello Rubies,
           Some time when we have huge query that it will be easy to use RAW SQL compare to ruby's active record query.

ActiveRecord::Base.connection.query <<-END
   /* Raw sql query */
  Select * from table where conditions
END

Custom connection and query

env = RAILS_ENV
config = YAML::load(File.open('config/database.yml'))
ActiveRecord::Base.establish_connection(config[env])

schemas = ActiveRecord::Base.connection.select_values("select * from pg_namespace where nspname not in ('public','information_schema') AND nspname NOT LIKE 'pg%'").inspect

Toggle div using ruby on rails

Hello Guys,
        Toggle is the function of prototype.js. We just have to use it to perform toggling.

eg.

html = "Toggle me <span id='collapse'>"
html << link_to_function(image_tag("arrow_normal.png", :style => 'border:none;'), "$('collapse').toggle();$('expand').toggle(); new Effect.Highlight('DIVID',{endcolor:'#ffffff', startcolor:'#ffffc8', duration:2.0}); return false;")
html << "</span>"    
html << "<span id='expand' style='display:none'>"
html << link_to_function(image_tag("arrow-down.png", :style => 'border:none;'), "$('collapse').toggle();$('expand').toggle();return false;")
html << "<div id='DIVID'> HELLO </div> </span>"

Initially it show the text as Toggle me with right arrow. but when you click on that i replace the right arrow to down arrow. And display the container area with text HELLO as per above eg.