Mon May 19 17:01:00 UTC 2008
script to find and replace a string in all files and subdirectories using shell programming
#!/bin/sh
if [ $# -ne 2 ]; then
echo 1>&2 Usage: $0 [string to replace] [replacement]
exit 127
fi
for file in $(find . -type f | grep -v .svn | grep -v .jpg | grep -v .gif | grep -v .tmp | grep -v .zip
| grep -v .png | grep -v .ttf | grep -v .psd | grep -v tempfile.tmp)
do
sed -e "s/$1/$2/g" $file > /tmp/tempfile.tmp
mv /tmp/tempfile.tmp $file
done
for file in $(find . | grep -v .svn | grep tempfile.tmp)
do
rm $file
done
Tue Feb 19 20:01:00 UTC 2008
selenium on rails
For rails 2 i was having problems getting selenium going. the upper left corner of my page was missing the layout. e.g.ActionController::MissingTemplate layout
change the file /vendor/plugins/selenium_on_rails/lib/controllers/selenium_controller.rb on line 22
to use layout => false
Fri Feb 01 17:39:00 UTC 2008
fast date values for fixtures
one of my annoyances while porting to rails 2 … and edge has to do with fixtures. they’ve decided to use the rathole plugin . . . but alas i was using the fixture_references plugin, and it seems they do not play nicely together. however, one thing i have learned is that i can do this:
3.weeks.ago.to_s(:db) => "2008-01-11 17:38:26" >> (Time.now + 3.weeks).to_s(:db) => "2008-02-22 17:38:42" >>
Tue Jan 29 13:48:00 UTC 2008
unitialized constant PASSTHROUGH exception in rails 2
You need to upgrade to ruby 1.8.6 . . .Wed Jan 09 10:44:00 UTC 2008
hacking irb and console for ruby and rails
Two things you need for your console. Imagine the following./script/console
>> helper.users_path
"/users"
(then highlight and copy to clipboard the following code.)
>> pastie
(browser opens to a pastie URL of the code you just copied to the clipboard
I also recommend installing wirble so you can have history in your console as well. ( sudo gem install wirble )
To get these helpful commands going in your irb or console session just add the following to your ~/.irbrc file.
# load rubygems and wirble
require 'net/http'
require 'rubygems' rescue nil
require 'wirble'
# load wirble
Wirble.init
Wirble.colorize
def Object.method_added(method)
return super(method) unless method == :helper
(class<<self;self;end).send(:remove_method, :method_added)
def helper(*helper_names)
returning $helper_proxy ||= Object.new do |helper|
helper_names.each { |h| helper.extend "#{h}_helper".classify.constantize }
end
end
helper.instance_variable_set("@controller", ActionController::Integration::Session.new)
def helper.method_missing(method, *args, &block)
@controller.send(method, *args, &block) if @controller && method.to_s =~ /_path$|_url$/
end
helper :application rescue nil
end if ENV['RAILS_ENV']
def pastie
url = URI.parse("http://pastie.caboo.se/pastes/create")
parameters = {}
IO.popen('pbpaste') do |clipboard|
parameters["paste[body]"] = clipboard.read
end
parameters["paste_parser"] = "ruby"
parameters["paste[authorization]"] = "burger"
pastie_url = Net::HTTP.post_form(url, parameters).body.match(/href="([^\"]+)"/)[1]
IO.popen('pbcopy', 'w+') do |clipboard|
clipboard.write(pastie_url)
end
pastie_url
system("open " + pastie_url)
end
Fri Jan 04 15:11:00 UTC 2008
Paypal SDK zip For Rails
the paypal rails SDK for Rails
It took me forever to find the software development kit for Paypal express checkout and rails. The downloads on this page have rails apps for most everything you could imagine doing with paypal and rails. hope this saves you some time looking for the link.
Wed Dec 12 11:16:00 UTC 2007
skitch for ruby developers
If you are developer using ruby a lot, or rails a lot, and find yourself in the irb console all the time, or trading code with other developers a lot I highly recommend utility belt .
I can type lines of code
>>def say_hi
>> puts ‘hi’ #note it autoindents for me here too
>>end
Copy that code, and then just type
>>pst
and then my browser opens up into a beautiful pastie url
If you are a designer and don’t have Skitch yet then you are missing out.
Mon Oct 15 04:02:00 UTC 2007
assert_hangs
another thing that rails needs for testing transactions and how large your alpha geek ego is:
assert_hung @me.hung_like_horse
....
but seriously.
how do you know that code such as
def some_controller @profile.save @user.profile = @profile @user.save endis all being done without interruption from some process like FERRET dying without an error?? what if you could do…
hangs_after(profile_save) assert_hung_after(profile_save)or something of the sort to test the atomicity of your controller ?
def some_controller
transaction.do
@profile.save
@user.profile = @profile
@user.save
end
end
Thu Sep 06 12:39:00 UTC 2007
testing your restful routes
script/console
>> irb ActionController::Routing::Routes
rs = ActionController::Routing::Routes
rs.named_routes.each {|name, route| puts( name, route) }; 1
Just copy the list of helper methods show in the output of step 4 and append path to that name. You must also provide the required ids shown in the output. For example take the output:
user GET /users/:id/ {:action=>”show”, :controller=>”users”}
and in the console, type:>> user_path 1 => "/users/1"The same process can be used for nested routes.
Reference: Rails Routing by David A Black. This book gave me a solid understanding of routes.
-taken from blog spot
Thu Aug 30 12:28:00 UTC 2007
ruby sql query syntax
Perusing some useful blogs – love err the blog. Thought I’d share the link. Another form of rails sql syntax
Mon Aug 13 14:36:00 UTC 2007
what ruby on rails needs
in one line what rails doesn’t have but needs
render :action => 'index', :anchor => 'signup'
Thu Aug 02 17:41:00 UTC 2007
Moving an older version of source to the head
This article is a minor reminder of what needs to happen in order to make an older version of source code in subversion come back (to be restored) to the head.
mkdir svntest
vi svntest/afile.rb
add the line version 1 exit
svn add svntest
svn ci -m “added version 1”
vi svntest/afile.rb
add the line
version 2
exit and then run
svn ci -m “added version 2”
now to merge back version 1 to head
if i try to do
svn merge -rhead:1471 http://svn.domain.com/svntest
then i get
Skipped missing target: ‘afile.rb’
but if do
cd svntest
svn merge -rhead:1471 http://svn.domain.com/svntest
i get
U afile.rb
sweet, lets check it in.
svn ci -m “changed to version 1”
i get
Sending svntest/afile.rb
Transmitting file data .
Committed revision 3.
cat afile.rb
version 1
yay!!
Thu Jun 14 11:31:00 UTC 2007
Using Scalable Acts As Taggable
Did some major updates the scalable acts as taggable plugin for rails. This should make people Happy. Thanks for the feed back on the agile web development page. I appreciate it.go to your rails project
cd /path/to/rails/project/
./script/plugin install svn://rubyforge.org/var/svn/scalabletagging/vendor/plugins/scalable_acts_as_taggable
The database needs two tables:
Taggings
`id` int(11) NOT NULL auto_increment,
`tag_id` int(11) default NULL,
`taggable_id` int(11) default NULL,
`taggable_type` varchar(255) default NULL,
`count` int(11) default '1',
#optional created_at dates
Tags
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`count` int(11) default '1',
#optional created_at dates
-CONSOLE-
matt-carrs-computer:/usr/htdocs/zivitalia junior$ ./script/console
Loading development environment.
>>
-MYSQL-
matt-carrs-computer:/usr/htdocs/zivitalia junior$ mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 198 to server version: 5.0.27-standard
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> select * from tags;
Empty set (0.00 sec)
mysql> select * from taggings;
Empty set (0.00 sec)
-CONSOLE-
>> p = Photoset.new(:photographer_id => 20, :name => 'bha', :status => 'Uploaded', :photographer_license_id => 3)
=> #
"photographer_license_id"=>3, "failed_processing_attempts"=>0, "processing"=>false, "created_at"=>nil,
"photographer_id"=>20}>
>> p.save
=> true
>> c = Compliment.new(:user_id => 1, :photoset_id => 1, :public => 1, :text => 'hshshhlk lkj')
=> #
"created_at"=>nil, "photoset_id"=>1}>
>> c.save
=> true
>> p.add_tag('cool, Awesome badass "This Rules"')
=> ["This Rules", "cool", "Awesome", "badass"]
-MYSQL-
mysql> select * from tags\G
*************************** 1. row ***************************
id: 17
name: this rules
count: 1
*************************** 2. row ***************************
id: 18
name: cool
count: 1
*************************** 3. row ***************************
id: 19
name: awesome
count: 1
*************************** 4. row ***************************
id: 20
name: badass
count: 1
4 rows in set (0.00 sec)
mysql> select * from taggings\G
*************************** 1. row ***************************
id: 21
tag_id: 17
taggable_id: 129
taggable_type: Photoset
count: 1
*************************** 2. row ***************************
id: 22
tag_id: 18
taggable_id: 129
taggable_type: Photoset
count: 1
*************************** 3. row ***************************
id: 23
tag_id: 19
taggable_id: 129
taggable_type: Photoset
count: 1
*************************** 4. row ***************************
id: 24
tag_id: 20
taggable_id: 129
taggable_type: Photoset
count: 1
4 rows in set (0.00 sec)
-CONSOLE-
>> p.add_tag('cool, awesome Badass "this rules"')
=> ["this rules", "cool", "awesome", "Badass"]
-MYSQL-
mysql> select * from tags\G
*************************** 1. row ***************************
id: 17
name: this rules
count: 2
*************************** 2. row ***************************
id: 18
name: cool
count: 2
*************************** 3. row ***************************
id: 19
name: awesome
count: 2
*************************** 4. row ***************************
id: 20
name: badass
count: 2
4 rows in set (0.00 sec)
mysql> select * from taggings\G
*************************** 1. row ***************************
id: 21
tag_id: 17
taggable_id: 129
taggable_type: Photoset
count: 2
*************************** 2. row ***************************
id: 22
tag_id: 18
taggable_id: 129
taggable_type: Photoset
count: 2
*************************** 3. row ***************************
id: 23
tag_id: 19
taggable_id: 129
taggable_type: Photoset
count: 2
*************************** 4. row ***************************
id: 24
tag_id: 20
taggable_id: 129
taggable_type: Photoset
count: 2
4 rows in set (0.00 sec)
-CONSOLE-
##NOTE - THIS IS c.add (COMPLIMENTS- not photosets)
>> c.add_tag('"This rules", badass, cool, AWESOME')
=> ["This rules", "badass", "cool", "AWESOME"]
-MYSQL-
mysql> select * from tags\G
*************************** 1. row ***************************
id: 17
name: this rules
count: 3
*************************** 2. row ***************************
id: 18
name: cool
count: 3
*************************** 3. row ***************************
id: 19
name: awesome
count: 3
*************************** 4. row ***************************
id: 20
name: badass
count: 3
4 rows in set (0.00 sec)
mysql> select * from taggings\G
*************************** 1. row ***************************
id: 21
tag_id: 17
taggable_id: 129
taggable_type: Photoset
count: 2
*************************** 2. row ***************************
id: 22
tag_id: 18
taggable_id: 129
taggable_type: Photoset
count: 2
*************************** 3. row ***************************
id: 23
tag_id: 19
taggable_id: 129
taggable_type: Photoset
count: 2
*************************** 4. row ***************************
id: 24
tag_id: 20
taggable_id: 129
taggable_type: Photoset
count: 2
*************************** 5. row ***************************
id: 25
tag_id: 17
taggable_id: 102
taggable_type: Compliment
count: 1
*************************** 6. row ***************************
id: 26
tag_id: 20
taggable_id: 102
taggable_type: Compliment
count: 1
*************************** 7. row ***************************
id: 27
tag_id: 18
taggable_id: 102
taggable_type: Compliment
count: 1
*************************** 8. row ***************************
id: 28
tag_id: 19
taggable_id: 102
taggable_type: Compliment
count: 1
8 rows in set (0.00 sec)
-CONSOLE-
>> Photoset.tag_counts
=> [#
"id"=>"18", "count"=>"2"}>, #
>> Compliment.tag_counts
=> [#
"id"=>"18", "count"=>"1"}>, #
>>
>> Photoset.find_tagged_with("cool")
=> [#
"photographer_license_id"=>"3", "failed_processing_attempts"=>"0", "id"=>"129", "processing"=>"0",
"created_at"=>"2007-06-13 23:14:33", "photographer_id"=>"20"}>]
>>
##NOTE-
tag_counts sends back an array of Tag objects. So i can do
tags = Photoset.tag_counts
for tag in tags
puts tag.count
puts tag.name
y Photoset.find_tagged_with(tag.name) #return all photosets with tagged with that tag.
end
##KEEP IN MIND
Taggings count keeps track of the number of times that CLASS instantiation has been tagged a certain tag.
Tags count keeps track of the number of times anything has been tagged that TAG.
