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.

Wednesday, August 29, 2012

undefined method

Hello Guys,
            When i was trying to configure browser cms along with ruby 1.8.7 and rails 2.3.11. It raise the error like `==': undefined method `name' for "abstract":String.

Than i have find the related solution. So follow below things.

Replace method ==(other) at gems/rails-2.3.11/lib/rails/gem_dependency.rb:277

def ==(other)
   if self.respond_to?(:name) && other.respond_to?(:name)
       self.name == other.name && self.requirement == other.requirement
    else
      if other.respond_to?(:requirement)
        self.requirement == other.requirement
      else
        false
      end
    end
end

Hope this will save you life :)

Saturday, August 4, 2012

Replace double space with   in javascript

Hello Guys,
        Last time when i was playing with double spaces.. found the interesting thing. When passing the double space string from javascript (view) to controller , it converts the valid space to non-breaking space.

Lets see the eg.

View: index.rhtml

a = "This  is the pen"
a.gsub("  ", "&nbsp;&nsbsp;")

<script language='javascript/text'>
 
  /* Now assign the value to input element by */
  $('txtid').value = <%= a %>;

 // Get value back encodeURIComponent($('txtid'))

</script>
 
at controller

def any_method
   # replace '\xC2\xA0' or '&nbsp;' with ' '
   params[:a].gsub("\xC2\xA0", " ").gsub("&nbsp;", " ")
end

Hope above article is helpful to you guys.

Disable right mouse click script

Hello Guys,
        Wanna to disable the right click? Follow the below JavaScript to achieve that.

eg.
test.html

<html>
  <body>
    <div id='img'>
       <%= image_tag('https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiH4hixyVqT9hmPJ3J2xBoC7972VOGveRP864H_eGjWZyeIrzxVdHs3GgshxDAS0LdvmtzFBmSxNw8yC3Plz7zUH2fE594sKTp2FTvbaITKcM1ajz8u8GTTMbol3KXUJLs5NPtdfLqc_A/s220-h/cutex.jpeg') %>
    </div>
  </body>
 
  <script language="javascript" type="text/javascript">
 
   // Disable right click 
   var message = "you can't right click on image";

   function clickIE4(){
     if (event.button == 2) {
        return false;
    }
  }

  function clickNS4(e){

    if (document.layers || document.getElementById && !document.all) {
   
      if (e.which == 2 || e.which == 3) {
          return false;
      }
    }
  }

  if (document.layers) {
    document.captureEvents(Event.MOUSEDOWN);
    document.onmousedown = clickNS4;
  }
  else
    if (document.all && !document.getElementById) {
      document.onmousedown = clickIE4;       
    }

   document.oncontextmenu = new Function("alert(message); return false")
  </script>
</html>