Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

PrettyConditions

// PrettyConditions allows you to easily print a report based on a condition for an ActiveRecord subclass

class ActiveRecord::Base
  def self.pretty_conditions(hash)
    meta_def hash[:name] do
      array = []
      puts "#{hash[:message]}:"
      puts "------------------"
      self.find(:all, :conditions => hash[:conditions]).each do |record|
        module_eval %[puts record.#{hash[:column_name]}]
        array << record
      end
      puts "------------------"
      puts "Total: #{array.size}"
      puts "------------------"
    end
  end
end


class Youth < ActiveRecord::Base
  pretty_conditions :name => 'unsponsored', :column_name => 'name', :message => "Unsponsored Youth", :conditions => "sponsor LIKE '%XXX%'"
  pretty_conditions :name => 'touchgroup_leaders', :column_name => 'name', :message => "Touchgroup Leaders", :conditions => ["touchgroup =?",'t']
  pretty_conditions :name => 'vegitarians', :column_name => 'name', :message => "Vegitarians", :conditions => ["vegitarian =?",'t'] 
end

Youth.unsponsored
Youth.touchgroup_leaders
Youth.vegitarians


Output:
saurasaurusrex:~/software cdcarter$ ruby actions.rb
Unsponsored Youth:
------------------
Hannah Gilbert
Jesse Lavercombe
Zach Adams
Danika Zabertini
------------------
Total: 4
------------------

Touchgroup Leaders:
------------------
Leland McKeeman
Marlee Leebrick-Stryker
Meggie Huges-Morrison
------------------
Total: 3
------------------

Vegitarians:
------------------
Marlee Leebrick-Stryker
Meggie Huges-Morrison
Amelia Nybakke
Ethan Edl
Maggie Heath
------------------
Total: 5
------------------

TxP-Rails Converter ERRORS

Here is the script I am trying to use that is throwing an error

require 'optparse'

class TXPMigrate
  attr_accessor :options
  
  def initialize
    self.options = {}
    self.parse_options
    self.convert_entries
  end

  
  def convert_entries
    txp_entries = ActiveRecord::Base.connection.select_all(%{
      SELECT
        ID,
        Title AS title,
        Body AS body,
        Posted AS created_at,
        (CASE Status WHEN '4' THEN "t" ELSE "f" END) AS active,
      FROM '#{self.options[:txp_db]}'.textpattern
    })
    
    puts "Converting #{txp_entries.size} entries.."
    
    txp_entries.each do |entry|
      a = Article.new
#      a.attributes = entry.reject { |k,v| k =~ /^(Category|ID)/ }
      a.save      
    end
  end

  def parse_options
    OptionParser.new do |opt|
      opt.banner = "Usage: textpattern.rb [options]"

      opt.on('--db DBNAME', String, 'Text Pattern database name.') { |d| self.options[:txp_db] = d }

      opt.on_tail('-h', '--help', 'Show this message.') do
        puts opt
        exit
      end

      opt.parse!(ARGV)
    end

    unless self.options.include?(:txp_db)
      puts "See textpattern.rb --help for help."
      exit
    end
  end
end

TXPMigrate.new
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS