Tuesday, July 2, 2013

Delayed job to use specific connection via ruby

Hello Guys,
           Yesterday  i was phasing one interesting problem. Sharing single database between 2 rails application but now i've requirement to use application specific table for delayed jobs.

Lets assume i've application called DemoApp & TestApp. And both sharing single DB called 'demo_app_prod'. Now for TestApp i required separate DB called 'test_app_prod' which has single table called 'delayed_jobs'.

Step1:

at TestApp you have database.yaml like
 login: &login
  adapter: mysql
  username: admin
  host: localhost  

  password:
 
development:
  <<: *login
  database:
demo_app_dev

test:
  <<: *login
  database:
demo_app_test
 
production:
  <<: *login
  database:
demo_app_prod  

staging:
  <<: *login
  database: test_app_prod


Step2:
at TestApp added migration called 
   rails generate migration add_delayed_job

  class AddDelayedJob < ActiveRecord::Migration
  def connection
    ActiveRecord::Base.establish_connection(Rails.env).connection
  end

 
  def up
    oldEnv = Rails.env
    Rails.env = 'staging' #set environment variable from your database.yml
    ActiveRecord::Base.establish_connection(Rails.env) 
  
    create_table :delayed_jobs, :force => true do |table|
      table.integer  :priority, :default => 0 

      table.integer  :attempts, :default => 0  
      table.text     :handler                      
      table.text     :last_error                  
      table.datetime :run_at                       
      table.datetime :locked_at                    
      table.datetime :failed_at                    
      table.string   :locked_by          
      table.timestamps
    end
    Rails.env = oldEnv
    ActiveRecord::Base.establish_connection ActiveRecord::Base.configurations[Rails.env]

  end
 
  def down   
    oldEnv = Rails.env
    Rails.env = '
staging'
    ActiveRecord::Base.establish_connection(Rails.env).connection

    drop_table :delayed_jobs
    Rails.env = oldEnv
    ActiveRecord::Base.establish_connection ActiveRecord::Base.configurations[Rails.env]

  end
end


Step3:

 In initializers/delayed_job.rb add below lines

Delayed::Job.class_eval do
  establish_connection ActiveRecord::Base.configurations["local"]
end


This the easy way to establish multiple db connection via ruby for delayed job.

Thursday, February 7, 2013

Rebuild the corrupted legacy data.


Hello Guys,
        Yesterday i phase one problem regarding positioning with legacy database. For my rails application i have used the awesome_nested_set to manage nesting/threading. Case is to rearrange the position and for that i have used pretty nice method called 'rebuild!'.

Assume i have model called 'Post' and have nesting per Category .

# id: integer
# category_id: integer
# lft :integer
# parent_id :integer
# position :integer
# rgt :integer
class Post < ActiveRecord::Base
  acts_as_nested_set :scope => :category
end

In the legacy database, having 3 categories called 'Alpha', 'Beta', 'Gama'. In which category Alpha's data mess out.
To over come this problem invoke command on console.
> script/console
> category = Category.find_by_name('Alpha')
> category.posts.rebuild!

Now cross check the data and it results as per need. Hope this post is helpful to you guys. Cheers!!

Tuesday, January 29, 2013

Integrate Bcms Blog along with BrowserCMS

Hello Guys,

        Want to integrate bcms_blog along with browser CMS via ruby? then simply follow the below steps.

http://modules.browsercms.org/modules/3-bcms-blog

Install gem called
gem install bcms_blog 

Specify configuration in environment.rb as
config.gem 'bcms_blog', :version=>'1.1.1'

Modify routes.rb
map.routes_for_bcms_blog
map.routes_for_browser_cms

No go to the your directory from command prompt and execute below commands.
script/generate browser_cms 
rake db:migrate

Now login to cms web page & go to Content Library Menu and on left side panel you'll see the Blog section and under that 3 sub sections.
 a. Blog
 b. Blog Comment
 c. Blog Post

Now creating and publishing blog follow procedure listed below:

1. Go to Blog section & create it.
2. Create category for blog section from Categorization -> Category Type..
3. Add blog post from Blog Post section.
4. Now publish all and on front end you'll see successfully created blog.
5. If you get any error like 
     ERROR: undefined method `_blog_post_path' for #<#:..> 
    Then
      include Cms::BlogHelper in ApplicationHelper
    And
     add  below method under Cms::BlogHelper
     def self.included(controller_class)
        Rails.logger.info "~~ BlogHelper included in #{ controller_class.inspect.to_s }"
     end 
6. Now on posted blog you have option for Comment.. and user's added comment only visible if they moderate it and publish.

Hope you guys, enjoy my blog.. and it's helpful .. For more information see https://github.com/browsermedia/bcms_blog/blob/master/doc/release_notes.txt