Replace <pre> tags with code fences
This commit is contained in:
parent
2ec9bf4b9a
commit
0af82ec66a
|
@ -12,19 +12,23 @@ Most notably, you want to install this gem if you get dropped MySQL connections
|
|||
|
||||
So we do:
|
||||
|
||||
<pre lang="bash">$ sudo gem install mysql
|
||||
```bash
|
||||
$ sudo gem install mysql
|
||||
...
|
||||
ERROR: Failed to build gem native extension.</pre>
|
||||
ERROR: Failed to build gem native extension.
|
||||
```
|
||||
|
||||
Normall this would install fine, but not on Mac OS X. This is because Mac OS X keeps its code, headers and libraries in odd places (compared to Linux). But don't panic. There's an easy solution to all this!
|
||||
<!--more-->
|
||||
|
||||
Just compile the gem yourself, and add some special ingredients. Well, not even that. Just compile it again like this:
|
||||
|
||||
<pre lang="bash">$ cd /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7
|
||||
```bash
|
||||
$ cd /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7
|
||||
$ sudo ruby extconf.rb --with-mysql-lib=/usr/local/mysql/lib --with-mysql-include=/usr/local/mysql/include
|
||||
$ sudo make
|
||||
$ sudo make install</pre>
|
||||
$ sudo make install
|
||||
```
|
||||
|
||||
Now, you have the ruby-mysql gem installed and ready to go! No more dropped MySQL connections for Rails!
|
||||
|
||||
|
|
|
@ -18,32 +18,40 @@ Luckily for me, I use capistrano. Here's how I fixed the problem.
|
|||
First of all, I created a partial that contains the revision number and render this in my layout.
|
||||
|
||||
app/views/layouts/_revision.rhtml:
|
||||
<pre lang="bash">
|
||||
```bash
|
||||
|
||||
CURRENT
|
||||
</pre>
|
||||
|
||||
```
|
||||
|
||||
This shows CURRENT always when I work with my working copy.
|
||||
|
||||
app/views/layouts/mylayout.rhtml
|
||||
<pre lang="ruby">
|
||||
```ruby
|
||||
|
||||
< %= render :partial => 'layout/revision' %>
|
||||
</pre>
|
||||
|
||||
```
|
||||
|
||||
Now, I assume you have already setup capistrano for your project and that you have a config/deploy.rb file.
|
||||
|
||||
I've added the following helper to my config/deploy.rb file:
|
||||
|
||||
<pre lang="ruby">desc "Write current revision to app/layouts/_revision.rhtml"
|
||||
```ruby
|
||||
desc "Write current revision to app/layouts/_revision.rhtml"
|
||||
task :publish_revision do
|
||||
run "svn info #{release_path} | grep ^Revision > #{release_path}/app/views/layouts/_revision.rhtml"
|
||||
end</pre>
|
||||
end
|
||||
```
|
||||
|
||||
Next, I added the following hook 'after_update_code'. This will automatically be run after update_code which is called in 'deploy'.
|
||||
|
||||
<pre lang="ruby">desc "Run this after update_code"
|
||||
```ruby
|
||||
desc "Run this after update_code"
|
||||
task :after_update_code do
|
||||
publish_revision
|
||||
end</pre>
|
||||
end
|
||||
```
|
||||
|
||||
That's it. When I deploy the application, the current code is checked out and and the layouts/_revision.rhtml file is overwritten with the current revision information.
|
||||
|
||||
|
|
|
@ -14,17 +14,23 @@ Here's a handy-dandy guide on how to merge your branch code with your trunk.
|
|||
|
||||
Firstly, make sure you have a working copy of your trunk. I choose to switch my working copy back: (oh, make sure you have all your changes checked in in your branch before you switch!)
|
||||
|
||||
<pre lang="bash">$ svn switch http://example.com/svn/myproject/trunk</pre>
|
||||
```bash
|
||||
$ svn switch http://example.com/svn/myproject/trunk
|
||||
```
|
||||
|
||||
This removes, adds and updates all files you have worked on in your branch and creates a working copy of the code in the trunk.
|
||||
|
||||
Now, with my trunk in place, I can call 'merge' and apply the changes.
|
||||
|
||||
<pre lang="bash">$ svn merge http://example.com/svn/myproject/trunk http://example.com/svn/myproject/branches/TRY-AJAX</pre>
|
||||
```bash
|
||||
$ svn merge http://example.com/svn/myproject/trunk http://example.com/svn/myproject/branches/TRY-AJAX
|
||||
```
|
||||
|
||||
Since the files from the trunk and the beginning of the TRY-branch are still exact copies, I won't get in any trouble. If you do (and you did change your code in your trunk), make sure to resolve merging problems before checking in. When ready, check in your new code!
|
||||
|
||||
<pre lang="bash">$ svn ci -m "Merge TRY-AJAX branch with trunk"</pre>
|
||||
```bash
|
||||
$ svn ci -m "Merge TRY-AJAX branch with trunk"
|
||||
```
|
||||
|
||||
That's it. You have now merged the branch with your trunk. Enjoy!
|
||||
|
||||
|
|
|
@ -19,7 +19,9 @@ First something about partials for those who don't know about it.
|
|||
|
||||
Partials are stored in files that start with an _ (underscore) and end in .rthml (or .rjs or .rxml) and you may include them in your ERb like this:
|
||||
|
||||
<pre lang="ruby">< %= render :partial => 'userinfo' %></pre>
|
||||
```ruby
|
||||
< %= render :partial => 'userinfo' %>
|
||||
```
|
||||
|
||||
This will include the partial _userinfo.rhtml for you. This way you can avoid duplication by including the same ERb template (partial) over and over again.
|
||||
|
||||
|
@ -27,7 +29,8 @@ Partials are also useful to keep your views (templates) clean and easy to unders
|
|||
|
||||
Let's say you have a profile page where you want to show a users's profile and some options for when the user is viewing his own profile. Also, you want to show a simple message when the users has not yet created a profile, which is a possibility in my application. The show.rhtml template for the profile may look like this
|
||||
|
||||
<pre lang="ruby"><h2>Profile for < %=h @profile.user.login %></h2>
|
||||
```ruby
|
||||
<h2>Profile for < %=h @profile.user.login %></h2>
|
||||
|
||||
< % if @profile.exists? -%>
|
||||
|
||||
|
@ -57,7 +60,8 @@ Let's say you have a profile page where you want to show a users's profile and s
|
|||
<li>< %= link_to "Change password", :controller => 'account', :action => "change_password" %></li>
|
||||
<li>< %= link_to "Edit your profile", profile_edit_url(:login => current_user.login) %>
|
||||
</li></ul>
|
||||
< % end -%></pre>
|
||||
< % end -%>
|
||||
```
|
||||
|
||||
As you may see, this is chaos. It's not easy to read and you can't easily change something when you need to. Wouldn't it be great to store all three seperate blocks (profile, no profile message and options) in partials?
|
||||
|
||||
|
@ -75,7 +79,8 @@ After you click OK, the partial will be created containing the code you selected
|
|||
|
||||
After all this the same ERb template looks like this:
|
||||
|
||||
<pre lang="ruby"><h2>Profile for < %=h @profile.user.login %></h2>
|
||||
```ruby
|
||||
<h2>Profile for < %=h @profile.user.login %></h2>
|
||||
|
||||
< % if @profile.exists? -%>
|
||||
< %= render :partial => 'profile' %>
|
||||
|
@ -85,7 +90,8 @@ After all this the same ERb template looks like this:
|
|||
|
||||
< % if @profile.user.id == current_user.id -%>
|
||||
< %= render :partial => 'options' %>
|
||||
< % end -%></pre>
|
||||
< % end -%>
|
||||
```
|
||||
|
||||
Now, isn't that more readable? Even, if you want to change a profile, you can just edit _profile.rhtml and you're done.
|
||||
|
||||
|
|
|
@ -11,8 +11,10 @@ You have put a lot of effort into creating a sexy overview of whatever data your
|
|||
How to go about that? Just printing the page with data is generally not a good idea because it has been optimized for display on a screen. The first step we need to take is adapting our page for printing. Stylesheets are very handy tools for this. Check the following part of the header of my layout:
|
||||
<!--more-->
|
||||
|
||||
<pre lang="ruby">stylesheet_link_tag('default', :media => :screen)
|
||||
stylesheet_link_tag('print', :media => :print)</pre>
|
||||
```ruby
|
||||
stylesheet_link_tag('default', :media => :screen)
|
||||
stylesheet_link_tag('print', :media => :print)
|
||||
```
|
||||
|
||||
This will generate HTML code that includes two stylesheets. However, only the 'default' stylesheet is used on screen. When the users prints a particular page, the print stylesheet is used instead. So, what do you want to change in the print stylesheet?
|
||||
|
||||
|
@ -35,7 +37,9 @@ Now, with all this in place, load your page with data. Print your page or watch
|
|||
|
||||
The final touch is adding a "Print this page" link to your navigation on screen. Here you can use link_to_function:
|
||||
|
||||
<pre lang="ruby">link_to_function("Print this Page", "javascript:print()")</pre>
|
||||
```ruby
|
||||
link_to_function("Print this Page", "javascript:print()")
|
||||
```
|
||||
|
||||
That's it. Just hit the Print this Page button and your browsers print dialog will pop up and use your fancy new print stylesheet.
|
||||
|
||||
|
|
|
@ -15,15 +15,21 @@ Well, Rails wouldn't be Rails if there weren't a generator for it!
|
|||
|
||||
This example is really easy. I want to create products in my database and expose them as HTML and XML to my users. Just do the following:
|
||||
|
||||
<pre lang="bash">$ ./script/generate scaffold_resource Product title:string description:text</pre>
|
||||
```bash
|
||||
$ ./script/generate scaffold_resource Product title:string description:text
|
||||
```
|
||||
|
||||
A migration is generated automatically so update that if you want to before running
|
||||
|
||||
<pre lang="bash">$ rake db:migrate</pre>
|
||||
```bash
|
||||
$ rake db:migrate
|
||||
```
|
||||
|
||||
As you can see in config/routes.rb, Rails has automatically generated routes for this resource!
|
||||
|
||||
<pre lang="ruby">map.resource :products</pre>
|
||||
```ruby
|
||||
map.resource :products
|
||||
```
|
||||
|
||||
Now you can start up your Rails app and access http://localhost:8000/products. Add some products if you like. Now if you got to http://localhost:8000/products/1 you a nice HTML scaffold. If you add .xml: http://localhost:8000/products/1.xml you'll get nice XML!
|
||||
|
||||
|
|
|
@ -15,84 +15,114 @@ I assume you have Rails 1.2.1 installed for this tutorial to work properly.
|
|||
|
||||
First, I create an new rails project named 'cookbook'. I use an SQLite3 database because it's easy to do so. You may use any Rails compatible database for this example.
|
||||
|
||||
<pre lang="bash">$ mkdir cookbook
|
||||
```bash
|
||||
$ mkdir cookbook
|
||||
rails --database sqlite3 cookbook
|
||||
cd cookbook</pre>
|
||||
cd cookbook
|
||||
```
|
||||
|
||||
First I create resource scaffolds for both the Recipe and Ingredient models:
|
||||
|
||||
<pre lang="bash">$ ./script/generate scaffold_resource Recipe title:string instructions:text
|
||||
./script/generate scaffold_resource Ingredient name:string quantity:string</pre>
|
||||
```bash
|
||||
$ ./script/generate scaffold_resource Recipe title:string instructions:text
|
||||
./script/generate scaffold_resource Ingredient name:string quantity:string
|
||||
```
|
||||
|
||||
As you can see I did not add a recipe_id to the ingredient model because of the has_many relationship. Add this column to the migration file. You should now be able to migrate your database:
|
||||
|
||||
<pre lang="bash">$ rake db:migrate</pre>
|
||||
```bash
|
||||
$ rake db:migrate
|
||||
```
|
||||
|
||||
If you add the recipe_id to the generate script the view for your ingredients will include a field for the recipe_id and that's not what you want.
|
||||
|
||||
Next, make the has_many relationship in your models.
|
||||
|
||||
app/models/recipe.rb:
|
||||
<pre lang="ruby">class Recipe < ActiveRecord::Base
|
||||
```ruby
|
||||
class Recipe < ActiveRecord::Base
|
||||
has_many :ingredients
|
||||
end</pre>
|
||||
end
|
||||
```
|
||||
|
||||
app/models/ingredient.rb
|
||||
</pre><pre lang="ruby">class Ingredient < ActiveRecord::Base
|
||||
</pre>```ruby
|
||||
class Ingredient < ActiveRecord::Base
|
||||
belongs_to :recipe
|
||||
end</pre>
|
||||
end
|
||||
```
|
||||
|
||||
So far, nothing new. Next we check out config/routes.rb:
|
||||
<pre lang="ruby">map.resources :ingredients
|
||||
map.resources :recipes</pre>
|
||||
```ruby
|
||||
map.resources :ingredients
|
||||
map.resources :recipes
|
||||
```
|
||||
|
||||
What we want is to map ingredients as a resource to recipes. Replace these two lines with:
|
||||
|
||||
<pre lang="ruby">map.resources :recipes do |recipes|
|
||||
```ruby
|
||||
map.resources :recipes do |recipes|
|
||||
recipes.resources :ingredients
|
||||
end</pre>
|
||||
end
|
||||
```
|
||||
|
||||
This will give you urls like /recipes/123/ingredients/321
|
||||
|
||||
Now we need to make some changes to the ingredients controller. Every ingredient belongs to a recipe. First add the filter:
|
||||
|
||||
<pre lang="ruby">before_filter(:get_recipe)
|
||||
```ruby
|
||||
before_filter(:get_recipe)
|
||||
|
||||
private
|
||||
def get_recipe
|
||||
@recipe = Recipe.find(params[:recipe_id])
|
||||
end</pre>
|
||||
end
|
||||
```
|
||||
|
||||
This will make sure that every ingredient knows what recipe it belongs to.
|
||||
|
||||
In the index method of the ingredient controller, make sure you have this:
|
||||
|
||||
<pre lang="ruby">@ingredients = @recipe.ingredients.find(:all)</pre>
|
||||
```ruby
|
||||
@ingredients = @recipe.ingredients.find(:all)
|
||||
```
|
||||
|
||||
This makes sure you only show ingredients for this recipe, and not all ingredients in the database.
|
||||
|
||||
Because we changed the route for ingredients, we need to update all ingredient_url() and ingredient_path() calls in our controller and views. Change all occurrences of
|
||||
|
||||
<pre lang="ruby">ingredient_url(@ingredient)</pre>
|
||||
```ruby
|
||||
ingredient_url(@ingredient)
|
||||
```
|
||||
and
|
||||
<pre lang="ruby">ingredient_path(@ingredient)</pre>
|
||||
```ruby
|
||||
ingredient_path(@ingredient)
|
||||
```
|
||||
|
||||
to
|
||||
|
||||
<pre lang="ruby">ingredient_url(@recipe, @ingredient)</pre>
|
||||
```ruby
|
||||
ingredient_url(@recipe, @ingredient)
|
||||
```
|
||||
and
|
||||
<pre lang="ruby">ingredient_path(@recipe, @ingredient)</pre>
|
||||
```ruby
|
||||
ingredient_path(@recipe, @ingredient)
|
||||
```
|
||||
|
||||
<em>Note: Make sure that you don't replace 'ingredient' with '@ingredient' in your views!
|
||||
|
||||
Add a link to the ingredients to your recipe's index.rhtml view.
|
||||
|
||||
<pre lang="ruby">link_to 'Ingredients', ingredients_path(recipe)</pre>
|
||||
```ruby
|
||||
link_to 'Ingredients', ingredients_path(recipe)
|
||||
```
|
||||
|
||||
And, at last, make sure the create method of your ingredients controller attaches the ingredient to the right recipe. Make sure the first two lines look like this:
|
||||
|
||||
<pre lang="ruby">def create
|
||||
@ingredient = @recipe.ingredients.new(params[:ingredient])</pre>
|
||||
```ruby
|
||||
def create
|
||||
@ingredient = @recipe.ingredients.new(params[:ingredient])
|
||||
```
|
||||
|
||||
You may now start your webserver and check out http://localhost:8000/recipes . Create some recipes and click 'ingredients'. You can now add ingredients for this recipe!
|
||||
|
||||
|
|
|
@ -38,7 +38,9 @@ That's it. Optionally you may want to adapt autoflickr.css to change the way the
|
|||
|
||||
If you want to include images in another place than the default, you may disable AutoFlickr for all pages and include the following PHP code in your template. Note that this code must be used within "The Loop".
|
||||
|
||||
<pre lang="php"><?php echo autoflickr_html(); ?></pre>
|
||||
```php
|
||||
<?php echo autoflickr_html(); ?>
|
||||
```
|
||||
|
||||
<h3>Demo</h3>
|
||||
|
||||
|
|
|
@ -10,15 +10,19 @@ This "Rails Tip Snippet" is one in a series of small blocks of code that will ma
|
|||
|
||||
This first snippet shows you how you can log informational message to your log file so you can track any important actions that happened. As an example you may want to log all user accounts that get deleted:
|
||||
<!--more-->
|
||||
<pre lang="ruby">def destroy
|
||||
```ruby
|
||||
def destroy
|
||||
@user = User.find(params[:id])
|
||||
@user.destroy!
|
||||
logger.info("User account '#{@user.login}' deleted by user #{current_user.login})
|
||||
...
|
||||
end</pre>
|
||||
end
|
||||
```
|
||||
Line 4 will put a string like "User account 'johnlocke" delete by user ariejan" in your log file. Use that information any way you want.
|
||||
|
||||
You may also use the logger to inspect variables:
|
||||
<pre lang="ruby">logger.info("article inspection: #{@article.inspect}")</pre>
|
||||
```ruby
|
||||
logger.info("article inspection: #{@article.inspect}")
|
||||
```
|
||||
Until next time! Any tips are welcome at <a href="mailto:tips@ariejan.net">tips@ariejan.net</a>.
|
||||
|
||||
|
|
|
@ -12,7 +12,9 @@ Users: John, Dick, Harry
|
|||
|
||||
With Ruby on Rails this is really easy. You probably have a collection of user objects. All you want is a list of names:
|
||||
|
||||
<pre lang="ruby">@users.collect{|u| u.name}.join(', ')</pre>
|
||||
```ruby
|
||||
@users.collect{|u| u.name}.join(', ')
|
||||
```
|
||||
|
||||
Read more <a href="http://ariejan.net/tags/tipsnippets/">Tip Snippets</a>?
|
||||
|
||||
|
|
|
@ -8,8 +8,10 @@ description = "How to fix this annoying Subversion problem."
|
|||
|
||||
After upgrading my Subversion server to Ubuntu Feisty, I noticed that when committing I got the following error:
|
||||
|
||||
<pre lang="text">svn: MERGE request failed on '/svn/repository/trunk'
|
||||
svn: MERGE of '/svn/repository/trunk': 200 OK (http://svn.myserver.com)</pre>
|
||||
```text
|
||||
svn: MERGE request failed on '/svn/repository/trunk'
|
||||
svn: MERGE of '/svn/repository/trunk': 200 OK (http://svn.myserver.com)
|
||||
```
|
||||
|
||||
Although the messages says that the commit failed, it has not. A simple 'svn update' will merge the changes you made to the repository to your working copy again and you're good to go.
|
||||
|
||||
|
|
|
@ -15,7 +15,8 @@ Before you jump in, make sure you have <a href="http://developer.apple.com/tools
|
|||
I'll also assume you have Ruby and rubygems installed and working already.
|
||||
|
||||
You will need to download, compile and install several graphics libraries that RMagick needs. Let's do this now.
|
||||
<pre lang="bash">curl -O http://download.savannah.gnu.org/releases/freetype/freetype-2.1.10.tar.gz
|
||||
```bash
|
||||
curl -O http://download.savannah.gnu.org/releases/freetype/freetype-2.1.10.tar.gz
|
||||
tar xzvf freetype-2.1.10.tar.gz
|
||||
cd freetype-2.1.10
|
||||
./configure --prefix=/usr/local
|
||||
|
@ -47,17 +48,22 @@ cd tiff-3.8.2
|
|||
./configure --prefix=/usr/local
|
||||
make
|
||||
sudo make install
|
||||
cd ..</pre>
|
||||
cd ..
|
||||
```
|
||||
Next we install ImageMagick:
|
||||
<pre lang="bash">curl -O http://easynews.dl.sourceforge.net/sourceforge/imagemagick/ImageMagick-6.3.0-0.tar.gz
|
||||
```bash
|
||||
curl -O http://easynews.dl.sourceforge.net/sourceforge/imagemagick/ImageMagick-6.3.0-0.tar.gz
|
||||
tar xzvf ImageMagick-6.3.0-0.tar.gz
|
||||
cd ImageMagick-6.3.0
|
||||
./configure --prefix=/usr/local
|
||||
make
|
||||
sudo make install
|
||||
cd ..</pre>
|
||||
cd ..
|
||||
```
|
||||
And now, ladies and gentlemen, what you've all been waiting for: RMagick:
|
||||
<pre lang="bash">sudo gem install --no-rdoc --no-ri RMagick</pre>
|
||||
```bash
|
||||
sudo gem install --no-rdoc --no-ri RMagick
|
||||
```
|
||||
In my case the generation of the documentation fails, so I tell rubygems not to compile the docs.
|
||||
|
||||
You now have RMagick installed on you Mac OS X 10.4.9 machine! Congratulations!
|
||||
|
|
|
@ -12,6 +12,8 @@ So, I investigated and found that by default ProFTPD tries to revolve the hostna
|
|||
|
||||
It's easy to stop ProFTPD from behaving like this by adding the following line to your proftpd.conf in /etc/proftpd:
|
||||
|
||||
<pre lang="text">IdentLookups off</pre>
|
||||
```text
|
||||
IdentLookups off
|
||||
```
|
||||
|
||||
Restart ProFTPD and you'll have a fast FTP connection to enjoy!
|
|
@ -12,15 +12,19 @@ I was happy, since trac 0.10.3 has many improvements over 0.9.x, but there was o
|
|||
|
||||
What happened? After upgrading the trac package, the plugins directory was emptied. Well, just re-install the WebAdmin plugin for 0.10.x.
|
||||
<!--more-->
|
||||
<pre lang="bash">cd /usr/share/trac/plugins
|
||||
```bash
|
||||
cd /usr/share/trac/plugins
|
||||
sudo svn co http://svn.edgewall.org/repos/trac/sandbox/webadmin/
|
||||
cd webadmin
|
||||
sudo python setup.py bdist_egg
|
||||
cd dist
|
||||
sudo easy_install-2.4 TracWebAdmin-0.1.2dev_r4429-py2.4.egg</pre>
|
||||
sudo easy_install-2.4 TracWebAdmin-0.1.2dev_r4429-py2.4.egg
|
||||
```
|
||||
That was easy, next I wanted to enable the plugin for all my trac installations by adding the proper configuration to /usr/share/trac/conf/trac.ini, the global trac configuration file that is used by all trac installs.
|
||||
<pre lang="text">[components]
|
||||
webadmin.* = enabled</pre>
|
||||
```text
|
||||
[components]
|
||||
webadmin.* = enabled
|
||||
```
|
||||
After restarting Apache (this is needed for some reason to get trac to read the new configuration file), no admin button showed up in any of the projects.
|
||||
|
||||
What went wrong is that Ubuntu (or Debian?) maintainers have changed the location of the global configuration file for trac. There are three solutions to this, all of them work fine, although I recommend you use the first one.
|
||||
|
@ -28,13 +32,17 @@ What went wrong is that Ubuntu (or Debian?) maintainers have changed the locatio
|
|||
<strong>1. Move your global configuration</strong>
|
||||
|
||||
The best way to tackle this problem is to move your global configuration file to the new location: /etc/trac/trac.ini
|
||||
<pre lang="bash">sudo mv /usr/share/trac/conf/trac.ini /etc/trac/trac.ini</pre>
|
||||
```bash
|
||||
sudo mv /usr/share/trac/conf/trac.ini /etc/trac/trac.ini
|
||||
```
|
||||
This way you're configuration is safe from new upgrades and confirms to the defaults the package maintainer has set.
|
||||
|
||||
<strong>2. Symlink the configuration</strong>
|
||||
|
||||
If for some reason you don't want to actually move your /usr/share/trac/conf/trac.ini file, you can create a symlink to the new location:
|
||||
<pre lang="bash">sudo ln -sf /usr/share/trac/conf/trac.ini /etc/trac/trac.ini</pre>
|
||||
```bash
|
||||
sudo ln -sf /usr/share/trac/conf/trac.ini /etc/trac/trac.ini
|
||||
```
|
||||
<pre>
|
||||
|
||||
This leaves your original configuration file in tact, but it may be removed by new upgrades.
|
||||
|
@ -42,10 +50,12 @@ This leaves your original configuration file in tact, but it may be removed by n
|
|||
<strong>3. Change Trac</strong>
|
||||
|
||||
You may also change the location where trac looks for the configuration file. Open up /var/lib/python-support/python2.5/trac/siteconfig.py and change the following:</pre>
|
||||
<pre lang="python">< __default_conf_dir__ = '/etc/trac'
|
||||
```python
|
||||
< __default_conf_dir__ = '/etc/trac'
|
||||
> __default_conf_dir__ = '/usr/share/trac/conf'[/pre]
|
||||
|
||||
(Note: the > and < symbols mark what is removed and what is added to the file.)
|
||||
|
||||
In any case, reboot your web server and you should be good to go again.</pre>
|
||||
In any case, reboot your web server and you should be good to go again.
|
||||
```
|
||||
|
||||
|
|
|
@ -11,6 +11,8 @@ There are times when you have a lot of data in a database (let's say wp_posts fo
|
|||
But, as I always say: <em>"You're a programmer! You should script the hell out of everything!"</em>
|
||||
|
||||
So, I found this: MySQL has built-in support to find and replace! Just a simple query will do:
|
||||
<pre lang="sql">UPDATE wp_posts set post_body = replace(post_body, 'needle', 'chocolate');</pre>
|
||||
```sql
|
||||
UPDATE wp_posts set post_body = replace(post_body, 'needle', 'chocolate');
|
||||
```
|
||||
That's it. The entire table 'wp_posts' is searched and all occurrences of "needle" are replaced with "chocolate". The query only took about a split second.
|
||||
|
||||
|
|
|
@ -12,20 +12,25 @@ This time I want to show you how you can easily send an e-mail from a BASH scrip
|
|||
|
||||
We're going to use the GNU Mail utility here. The basic syntax to send an email is like this:
|
||||
|
||||
<pre lang="bash">/usr/bin/mail -s "Subject" someone@example.org < message.txt</pre>
|
||||
```bash
|
||||
/usr/bin/mail -s "Subject" someone@example.org < message.txt
|
||||
```
|
||||
The trick when using this in a shell script is creating and using the message.txt file correctly.
|
||||
|
||||
Let's setup the basis first:
|
||||
<pre lang="bash">#!/bin/bash
|
||||
```bash
|
||||
#!/bin/bash
|
||||
SUBJECT="Automated Security Alert"
|
||||
TO="alarms@ariejan.net"
|
||||
MESSAGE="/tmp/message.txt"
|
||||
|
||||
/usr/bin/mail -s "$SUBJECT" "$TO" < $MESSAGE</pre>
|
||||
/usr/bin/mail -s "$SUBJECT" "$TO" < $MESSAGE
|
||||
```
|
||||
|
||||
All we need to do now is create the message. In this example we're going to notify the receiver that something happened at a certain time. We can use the append (>>) operator to add text to the message file. Afterwards, we must remove the temporary message file, of course. The complete script now becomes:
|
||||
|
||||
<pre lang="bash">#!/bin/bash
|
||||
```bash
|
||||
#!/bin/bash
|
||||
SUBJECT="Automated Security Alert"
|
||||
TO="alarms@ariejan.net"
|
||||
MESSAGE="/tmp/message.txt"
|
||||
|
@ -35,7 +40,8 @@ echo "Time: `date`" >> $MESSAGE
|
|||
|
||||
/usr/bin/mail -s "$SUBJECT" "$TO" < $MESSAGE
|
||||
|
||||
rm $MESSAGE</pre>
|
||||
rm $MESSAGE
|
||||
```
|
||||
|
||||
The email will contain the a timestamp from when the mail was sent.
|
||||
|
||||
|
|
|
@ -13,7 +13,8 @@ Creating a new SVN repository and Trac installation every time is quite boring a
|
|||
The result is the following BASH script. It takes one argument, the name of the project you want to create. E.g if you wanted to create a SVN repository and trac installation for "My Project" you would run the following command:
|
||||
<pre>$ ./create_dev_env my_project</pre>
|
||||
The script it self looks like this:
|
||||
<pre lang="bash">#!/bin/sh
|
||||
```bash
|
||||
#!/bin/sh
|
||||
echo == Creating Subversion and Trac installation for $1
|
||||
echo = Creating SVN Repository...
|
||||
|
||||
|
@ -58,7 +59,8 @@ trac-admin /var/lib/trac/$1 permission add ariejan TRAC_ADMIN
|
|||
chown -R www-data:www-data /var/lib/trac/$1
|
||||
|
||||
echo
|
||||
echo == Done.</pre>
|
||||
echo == Done.
|
||||
```
|
||||
First it creates the SVN directory in /var/lib/svn/my_project and creates repository and adds the trac post-commit hook for trac integration.
|
||||
|
||||
Next, it creates the trac installation in /var/lib/trac/my_project and removes all the persmission the anonymous users has. (You may want to remove these lines for open source or public projects.)
|
||||
|
|
|
@ -17,11 +17,15 @@ The problem was, that my router was caching parts from the old and parts from th
|
|||
What you need to do is login to the admin panel of your router and enable SSH access (for your own network, not for the outside world!).
|
||||
|
||||
You SSH to your route, probably as the 'root' or 'admin' user. In this case the IP is your routers IP address.
|
||||
<pre lang="bash">$ ssh -l admin 10.0.0.1</pre>
|
||||
```bash
|
||||
$ ssh -l admin 10.0.0.1
|
||||
```
|
||||
Once you're in, you can issue the killall command to issue the 'HUP' signal to DNSMasq. This does not kill your DNSMasq, but tells it to flush the cache an reread its configuration, thus starting over with a clean slate.
|
||||
|
||||
The 'HUP' signal has the number '1' so you can run the following command once logged in:
|
||||
<pre lang="bash">$ killall -1 dnsmasq</pre>
|
||||
```bash
|
||||
$ killall -1 dnsmasq
|
||||
```
|
||||
Now you can logout, and notice that your cache was indeed cleared.
|
||||
|
||||
This method should work on every Linux based router that uses DNSMasq and support SSH logins.
|
||||
|
|
|
@ -13,9 +13,13 @@ I think that, if you're a web developer, you've seen the <a href="http://code.go
|
|||
That's, of course, all very nice, but you should be able to plug it in into your Rails app. And now you can!
|
||||
|
||||
Install my plugin:
|
||||
<pre lang="bash">./script/plugin install http://svn.ariejan.net/plugins/blueprint</pre>
|
||||
```bash
|
||||
./script/plugin install http://svn.ariejan.net/plugins/blueprint
|
||||
```
|
||||
And then generate as many BlueprintCSS layouts as you'd like. To create a layout for your posts controller, simply run the following command:
|
||||
<pre lang="bash">./script/generate blueprint posts</pre>
|
||||
```bash
|
||||
./script/generate blueprint posts
|
||||
```
|
||||
This will create a posts.rhtml template in app/views/layouts, and add the proper CSS and images to your application. That's all!
|
||||
|
||||
Note 1: You may remove a few lines of inline CSS from your layout to remove the supporting background images.
|
||||
|
|
|
@ -8,13 +8,17 @@ description = "Some nifty Rails trick using content_for and yield."
|
|||
|
||||
You may have heard of a very nice Rails technique that used content_for and yield to stuff custom blocks of content into a layout. For example, in a view you could add a block like this:
|
||||
|
||||
<pre lang="html"><% content_for :sidebar do %>
|
||||
```html
|
||||
<% content_for :sidebar do %>
|
||||
This goes into the sidebar when viewing this action!
|
||||
<% end %></pre>
|
||||
<% end %>
|
||||
```
|
||||
|
||||
It doesn't matter where you put it in the view, because, as you may notice, the content within the content_for block is not shown in the view. The layout can pull the content for :sidebar and show it in a custom place, mostly the sidebar:
|
||||
|
||||
<pre lang="html"><%= yield :sidebar %></pre>
|
||||
```html
|
||||
<%= yield :sidebar %>
|
||||
```
|
||||
|
||||
Nice, you now don't have to hard-code the sidebar (and it's layout elements) in your views everytime. You can even move the sidebar to another place, without breaking any of your views. How great is that?
|
||||
|
||||
|
@ -22,7 +26,9 @@ What does break your views, however, is not assigning anything to :sidebar. If y
|
|||
|
||||
So, how can you solve this? Quite easily, actually. What you want, probably, is display a default sidebar when no custom sidebar has been assigned. you can do this with the following line of über sexy Ruby code:
|
||||
|
||||
<pre lang="html"><%= (sidebar = yield :sidebar) ? sidebar : render(:partial => 'shared/sidebar') %></pre>
|
||||
```html
|
||||
<%= (sidebar = yield :sidebar) ? sidebar : render(:partial => 'shared/sidebar') %>
|
||||
```
|
||||
|
||||
First, you grab the :sidebar content and stuff it into a variable named, you guessed it, 'sidebar'. If this sidebar containts anything, show it. If it doesn't, you render a partial in the app/views/shared directory named _sidebar.rhtml.
|
||||
|
||||
|
|
|
@ -13,6 +13,8 @@ Well, that's all nice, but what if you notice that your flash is not cleared, an
|
|||
The reason your flash is not cleared is that it will only be cleared if you render the current action, or if you redirect to another action. If you render another action or template, the flash will not be cleared out.
|
||||
|
||||
Solving this is quite easy, actually. You only need to know how to do it. Use the following code in combination with render to show it only for the current action:
|
||||
<pre lang="ruby">flash.now[:notice] = "Render, render! This will only show up once."</pre>
|
||||
```ruby
|
||||
flash.now[:notice] = "Render, render! This will only show up once."
|
||||
```
|
||||
That's all there is to it really.
|
||||
|
||||
|
|
|
@ -8,7 +8,9 @@ description = "How to get the number of messages in your Postfix queue."
|
|||
|
||||
Got bash? Here's a nice snippet that will return the number of messages currently in the postfix queue.
|
||||
|
||||
<pre lang="bash">postqueue -p | tail -n 1 | cut -d' ' -f5</pre>
|
||||
```bash
|
||||
postqueue -p | tail -n 1 | cut -d' ' -f5
|
||||
```
|
||||
|
||||
Feel free to post any updates or improvements.
|
||||
|
||||
|
|
|
@ -10,11 +10,15 @@ Sometimes it's necessary to set the starting point of a MySQL auto-increment val
|
|||
|
||||
Normally, MySQL starts auto-incrementing at 1. But let's say you want to start at 10.000, because you want at least a five figure number. You can use the following query to set the MySQL auto-index:
|
||||
|
||||
<pre lang="sql">ALTER TABLE some_table AUTO_INCREMENT=10000</pre>
|
||||
```sql
|
||||
ALTER TABLE some_table AUTO_INCREMENT=10000
|
||||
```
|
||||
|
||||
If you want to delete all records from your table and restart auto-index at 1, you might be tempted to run a DELETE query, followed by the above example, setting the auto increment value to 1. There is a shortcut, however:
|
||||
|
||||
<pre lang="sql">TRUNCATE TABLE some_table</pre>
|
||||
```sql
|
||||
TRUNCATE TABLE some_table
|
||||
```
|
||||
|
||||
This will basically reset the table, deleting all data and resetting the auto increment index. Do not that the truncate command is a hard-reset option. For instance, any triggers "ON DELETE" will not be fired when using truncate.
|
||||
|
||||
|
|
|
@ -10,30 +10,38 @@ Sometimes you're working on a Rails project and you think: "hey! This should be
|
|||
|
||||
I'm using the <a href="http://juixe.com/svn/acts_as_rateable">acts_as_rateable plugin</a> which creates an extra database table containing all ratings. I also have a table with my objects. Using the plugin I'm now able to do the following:
|
||||
|
||||
<pre lang="ruby">obj = Object.find(:first)
|
||||
```ruby
|
||||
obj = Object.find(:first)
|
||||
obj.add_rating Rating.new(:rating => 4)
|
||||
obj.add_rating Rating.new(:rating => 5)
|
||||
obj.rating
|
||||
=> 4.5</pre>
|
||||
=> 4.5
|
||||
```
|
||||
|
||||
This works all perfectly, until you want to sort objects by rating. You could construct a huge SQL query to join the two tables, but that's not really efficient, especially when your database gets bigger.
|
||||
|
||||
The solution is very easy and even more elegant. Use a cache! For this, you'll first have to add a new field to the objects table. Do this in a migration:
|
||||
|
||||
<pre lang="ruby">add_column :objects, :rating_cache, :float</pre>
|
||||
```ruby
|
||||
add_column :objects, :rating_cache, :float
|
||||
```
|
||||
|
||||
Now, in the Object model, add the following method:
|
||||
|
||||
<pre lang="ruby">def rate_with(rating)
|
||||
```ruby
|
||||
def rate_with(rating)
|
||||
add_rating(rating)
|
||||
update_attribute('rating_cache', self.rating)
|
||||
end</pre>
|
||||
end
|
||||
```
|
||||
|
||||
You'll need to change your controller from using #add_rating to #rate_with. The syntax is exactly the same. Now, when you add a rating, we also store the average rating in the rating_cache column.
|
||||
|
||||
To get back to the sorting problem, you can now use the rating_cache column to sort Objects.
|
||||
|
||||
<pre lang="ruby">Object.find(:all, :order => 'rating_cache DESC')</pre>
|
||||
```ruby
|
||||
Object.find(:all, :order => 'rating_cache DESC')
|
||||
```
|
||||
|
||||
Of course, you can use this trick on all sorts of relations. Have fun with it.
|
||||
|
||||
|
|
|
@ -12,25 +12,33 @@ I know, there are tons of fancy apps (with a decent GUI) that allow you to creat
|
|||
|
||||
All you really need to burn a DVD-Video is one packages: dvd+rw-tools. If you're on Debian or Ubuntu, just run this:
|
||||
|
||||
<pre lang="bash">apt-get install dvd+rw-tools</pre>
|
||||
```bash
|
||||
apt-get install dvd+rw-tools
|
||||
```
|
||||
|
||||
So, I have this VIDEO_TS folder and a blank DVD in my DVD driver (which is known to my system as /dev/sr0). To make a correct DVD, you'll also need an AUDIO_TS folder. It doesn't matter if it's empty, it should be there.
|
||||
|
||||
<em>I'm assuming you're all doing this as root. If not, just add 'sudo' in front of every command you perform</em>
|
||||
|
||||
<pre lang="bash">mkdir AUDIO_TS</pre>
|
||||
```bash
|
||||
mkdir AUDIO_TS
|
||||
```
|
||||
|
||||
Next, we need to set the correct permissions on the VIDEO_TS, AUDIO_TS and the files contained in these directories.
|
||||
|
||||
<pre lang="bash">chown -R root:root VIDEO_TS AUDIO_TS
|
||||
```bash
|
||||
chown -R root:root VIDEO_TS AUDIO_TS
|
||||
chmod 500 VIDEO_TS AUDIO_TS
|
||||
chmod 400 VIDEO_TS/*</pre>
|
||||
chmod 400 VIDEO_TS/*
|
||||
```
|
||||
|
||||
Well, you already have all your files prepped, and you're good to go.
|
||||
|
||||
The following command will burn you a nice video DVD. Make sure a blank DVD is inserted into your drive. As I said before, my drive is located at /dev/sr0. You system is probably different. Check your boot log (dmesg) for messages of a dvd driver.
|
||||
|
||||
<pre lang="bash">growisofs -dvd-compat -Z /dev/sr0 -dvd-video .</pre>
|
||||
```bash
|
||||
growisofs -dvd-compat -Z /dev/sr0 -dvd-video .
|
||||
```
|
||||
|
||||
It's very important not to forget the tailing dot, it tells growisofs that you want to use the current directory as the source for your DVD. Depending on the speed of your burner, you'll have a new DVD to watch in a few minutes. Enjoy! And a happy 2008!
|
||||
|
||||
|
|
|
@ -11,7 +11,8 @@ Now, email notification are nothing exotic. You don't need any plugins, just an
|
|||
|
||||
In your trac directory open up conf/trac.ini and look for the [notification] header. Make sure you have at least the following settings. Of course, make sure you enter valid values for your situation.
|
||||
|
||||
<pre lang="text">always_notify_owner = true
|
||||
```text
|
||||
always_notify_owner = true
|
||||
always_notify_reporter = true
|
||||
always_notify_updater = true
|
||||
smtp_always_cc = trac-updates@mailinglist # Optional if you want to archive outgoing emails
|
||||
|
@ -22,7 +23,8 @@ smtp_replyto = noreply@ariejan.net
|
|||
smtp_user =
|
||||
smtp_password =
|
||||
smtp_server = localhost
|
||||
smtp_port = 25</pre>
|
||||
smtp_port = 25
|
||||
```
|
||||
|
||||
There are a few more options like 'ignore_domains' if you don't want to send emails to certain domains.
|
||||
|
||||
|
|
|
@ -7,7 +7,9 @@ slug = "debian-etch-rmagick-loaderror"
|
|||
|
||||
If you're on Debian Etch, you may encounter the following error
|
||||
|
||||
<pre lang="text">libMagickWand.so.1: cannot open shared object file: No such file or directory - /usr/lib/ruby/gems/1.8/gems/rmagick-2.3.0/lib/RMagick2.so</pre>
|
||||
```text
|
||||
libMagickWand.so.1: cannot open shared object file: No such file or directory - /usr/lib/ruby/gems/1.8/gems/rmagick-2.3.0/lib/RMagick2.so
|
||||
```
|
||||
|
||||
This basically means that the libMagickWand.so.1 file cannot be found. However, it is available on your system. All you need to do to fix it, is tell your box to look in the right place for the file.
|
||||
|
||||
|
@ -15,11 +17,15 @@ To fix this issue once and for all, open up /etc/ld.so.conf.d/whatever_file_is_h
|
|||
|
||||
In this file, add the following line at the bottom
|
||||
|
||||
<pre lang="text">/usr/local/lib</pre>
|
||||
```text
|
||||
/usr/local/lib
|
||||
```
|
||||
|
||||
Save the file and next run the 'ldconfig' command. This will reread the configuration file you just edited. Now, restart your Rails app and you'll notice the error is gone and all is good again.
|
||||
|
||||
<pre lang="sh">ldconfig</pre>
|
||||
```sh
|
||||
ldconfig
|
||||
```
|
||||
|
||||
This change will be kept after you reboot, so you won't encounter this error any time soon again.
|
||||
|
||||
|
|
|
@ -11,7 +11,8 @@ I'm running WordPress with Apache2, so it shouldn't be too hard redirect those o
|
|||
|
||||
Here is the default .htaccess file generated by WordPress:
|
||||
|
||||
<pre lang="text"># BEGIN WordPress
|
||||
```text
|
||||
# BEGIN WordPress
|
||||
<IfModule mod_rewrite.c>
|
||||
RewriteEngine On
|
||||
RewriteBase /
|
||||
|
@ -20,13 +21,15 @@ RewriteCond %{REQUEST_FILENAME} !-d
|
|||
RewriteRule . /index.php [L]
|
||||
</IfModule>
|
||||
|
||||
# END WordPress</pre>
|
||||
# END WordPress
|
||||
```
|
||||
|
||||
As I said, it's <strong>generated</strong> by WordPress. It would be very unwise to edit this, because our changes might get lost in the future.
|
||||
|
||||
The solution is simple. Add another block above the generated one that does permanent redirects. In this example I rewrite <a href="http://ariejan.net/pages/svnsheet">pages/svnsheet</a> to <a href="http://ariejan.net/svncheatsheet">http://ariejan.net/svncheatsheet</a>.
|
||||
|
||||
<pre lang="text"><IfModule mod_rewrite.c>
|
||||
```text
|
||||
<IfModule mod_rewrite.c>
|
||||
RewriteEngine On
|
||||
RewriteBase /
|
||||
RewriteCond %{REQUEST_FILENAME} !-f
|
||||
|
@ -43,7 +46,8 @@ RewriteCond %{REQUEST_FILENAME} !-d
|
|||
RewriteRule . /index.php [L]
|
||||
</IfModule>
|
||||
|
||||
# END WordPress</pre>
|
||||
# END WordPress
|
||||
```
|
||||
|
||||
FYI, the R=301 means the browser (or search bot) receives a "Moved Permanently" message. L means this is the <strong>l</strong>ast rewrite rule to read.
|
||||
|
||||
|
|
|
@ -11,13 +11,17 @@ Let's say we want to recompile mod_python for apache 2 to hook in to python 2.5,
|
|||
|
||||
First, get every thing installed you may need to build the libapache2-mod-python package.
|
||||
|
||||
<pre lang="sh">$ apt-get build-dep libapache2-mod-python</pre>
|
||||
```sh
|
||||
$ apt-get build-dep libapache2-mod-python
|
||||
```
|
||||
|
||||
Okay, next let's grab the source for the package. The source will be unpacked to your current working directory, so it may be a good idea to create a seperate directory for this.
|
||||
|
||||
<pre lang="sh">$ mkdir src
|
||||
```sh
|
||||
$ mkdir src
|
||||
$ cd src
|
||||
$ apt-get source libapache2-mod-python</pre>
|
||||
$ apt-get source libapache2-mod-python
|
||||
```
|
||||
|
||||
In the case of this example, you don't need to do anything special to get python 2.5 linked. Just install the python2.5 and python2.5-dev packages.
|
||||
|
||||
|
@ -25,14 +29,18 @@ In the case of this example, you don't need to do anything special to get python
|
|||
|
||||
Okay, now go to the source directory and build the package. This is the tricky command here:
|
||||
|
||||
<pre lang="sh">$ cd libapache2-mod-python
|
||||
$ dpkg-buildpackage -rfakeroot -b</pre>
|
||||
```sh
|
||||
$ cd libapache2-mod-python
|
||||
$ dpkg-buildpackage -rfakeroot -b
|
||||
```
|
||||
|
||||
This will build the package. You will get some warnings and errors about missing GPG keys. This is okay. You are not the package maintainer, so your packages should not be marked as 'original'.
|
||||
|
||||
You're now ready to install your compiled package.
|
||||
|
||||
<pre lang="sh">dpkg -i ../libapache2-mod-python-3.3.1-3-i386.deb</pre>
|
||||
```sh
|
||||
dpkg -i ../libapache2-mod-python-3.3.1-3-i386.deb
|
||||
```
|
||||
|
||||
That's all! You compiled and installed a package from source!
|
||||
|
||||
|
|
|
@ -9,26 +9,32 @@ Migrations have up and down methods, as we all know. But in some cases, your up
|
|||
|
||||
For example:
|
||||
|
||||
<pre lang="ruby">def self.up
|
||||
```ruby
|
||||
def self.up
|
||||
# Change the zipcode from the current :integer to a :string type.
|
||||
change_column :address, :zipcode, :string
|
||||
end</pre>
|
||||
end
|
||||
```
|
||||
|
||||
Now, converting integers to strings will always work. But, you feel it coming, converting a string into an integer will not always be possible. In other words, we can't reverse this migration.
|
||||
|
||||
That's why we should raise ActiveRecord::IrreversibleMigration in the down method.
|
||||
|
||||
<pre lang="ruby">def self.down
|
||||
```ruby
|
||||
def self.down
|
||||
raise ActiveRecord::IrreversibleMigration
|
||||
end</pre>
|
||||
end
|
||||
```
|
||||
|
||||
Now, if you run your migration (upwards), you'll see it being applied like it shoud. However, if you try to go back, you'll see rake aborting with ActiveRecord::IrreversibleMigration.
|
||||
|
||||
<pre lang="sh">$ rake db:migrate VERSION=4
|
||||
```sh
|
||||
$ rake db:migrate VERSION=4
|
||||
-- Database is migrated
|
||||
$ rake db:migrate VERSION=3
|
||||
-- Rake aborted!
|
||||
-- ActiveRecord::IrreversibleMigration</pre>
|
||||
-- ActiveRecord::IrreversibleMigration
|
||||
```
|
||||
|
||||
So, if you have things you can't undo, raise ActiveRecord::IrreversibleMigration in your migration's down method.
|
||||
|
||||
|
|
|
@ -15,7 +15,8 @@ The ActsAsGold Gem allows you to extend your ActiveRecord model with this money
|
|||
|
||||
Let me give you a small tour.
|
||||
|
||||
<pre lang="ruby">class Player < ActiveRecord::Base
|
||||
```ruby
|
||||
class Player < ActiveRecord::Base
|
||||
acts_as_gold :column => :money
|
||||
end
|
||||
|
||||
|
@ -26,7 +27,8 @@ end
|
|||
|
||||
# You can also easily earn or spend money
|
||||
@player.earn(10.gold + 75.silver)
|
||||
@player.spend(1.gold + 10.silver + 95.copper)</pre>
|
||||
@player.spend(1.gold + 10.silver + 95.copper)
|
||||
```
|
||||
|
||||
<a href="http://github.com/ariejan/acts_as_gold/tree/master/README.textile">Read more about the Gem</a>, or install the gem right now:
|
||||
|
||||
|
@ -46,12 +48,14 @@ WA (WarcraftArmory) allows you to easily retrieve character information from the
|
|||
|
||||
It works for both EU and US warcraft servers.
|
||||
|
||||
<pre lang="ruby">require 'warcraft_armory'
|
||||
```ruby
|
||||
require 'warcraft_armory'
|
||||
|
||||
character = WarcraftArmory.find(:eu, 'Aszune', 'Nosius')
|
||||
|
||||
character.race => "Human"
|
||||
character.level => 15</pre>
|
||||
character.level => 15
|
||||
```
|
||||
|
||||
Again, simply install the plugin and use it like any other gem or <a href="http://github.com/ariejan/warcraft_armory/tree/master/README.textile">read the README</a> first.
|
||||
|
||||
|
|
|
@ -9,11 +9,13 @@ How ofter were you asked by a client for a CSV (or excel) file with data from th
|
|||
|
||||
Let's say you want to export the id, name and email fields from your users table to a CSV file. Here is your code:
|
||||
|
||||
<pre lang="sql">SELECT id, name, email INTO OUTFILE '/tmp/result.csv'
|
||||
```sql
|
||||
SELECT id, name, email INTO OUTFILE '/tmp/result.csv'
|
||||
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
|
||||
ESCAPED BY ‘\\’
|
||||
LINES TERMINATED BY '\n'
|
||||
FROM users WHERE 1</pre>
|
||||
FROM users WHERE 1
|
||||
```
|
||||
|
||||
Well, if you know MySQL, you'll know how to customize this query to spit out the the right data. Your csv file can be found in /tmp/result.csv
|
||||
|
||||
|
|
|
@ -11,14 +11,16 @@ Most likely you can't wait to get started with these new features, especially wh
|
|||
|
||||
Let's go...
|
||||
|
||||
<pre lang="sh">
|
||||
```sh
|
||||
|
||||
mkdir -p myapp/vendor
|
||||
cd myapp
|
||||
git init
|
||||
git submodule add git://github.com/rails/rails.git vendor/rails
|
||||
git commit -m "Frozen Rails Edge as submodule"
|
||||
ruby vendor/rails/railties/bin/rails .
|
||||
# Add generated files to git, and code on...</pre>
|
||||
# Add generated files to git, and code on...
|
||||
```
|
||||
|
||||
First, you create a new directory for your app, including the <code>vendor</code> directory. Easy, right?
|
||||
|
||||
|
@ -38,23 +40,29 @@ That's all, you now have a new Rails Edge application. Try <code>ruby script/ser
|
|||
|
||||
At some point you'll push your <code>myapp</code> project to a remote git server. When you clone a fresh copy, you'll have to initialize the git submodules. This is quite easy:
|
||||
|
||||
<pre lang="sh">
|
||||
```sh
|
||||
|
||||
git submodule init
|
||||
git submodule update</pre>
|
||||
git submodule update
|
||||
```
|
||||
|
||||
<strong>Updating Rails Edge</strong>
|
||||
|
||||
As I said earlier, Git will not keep your submodules up-to-date for you, but will stick with the revision you added. To keep track of Rails Edge's progress, you'll need to update the submodule. This is done like this:
|
||||
|
||||
<pre lang="sh">cd myapp/vendor/rails
|
||||
```sh
|
||||
cd myapp/vendor/rails
|
||||
git remote update
|
||||
git merge origin/master</pre>
|
||||
git merge origin/master
|
||||
```
|
||||
|
||||
This will update your Rails Edge code. Make a commit, stating you updated the code!
|
||||
|
||||
After updating Rails Edge, you may want to update your rails application (like javascript files, config files etc).
|
||||
|
||||
<pre lang="sh">rake rails:update</pre>
|
||||
```sh
|
||||
rake rails:update
|
||||
```
|
||||
|
||||
Good luck! And happy coding!
|
||||
|
||||
|
|
|
@ -13,12 +13,16 @@ This is an early version that allows the retrieval of character information from
|
|||
|
||||
Feel free to check-out the <a href="http://github.com/ariejan/warcraft-armory">code</a>, read the <a href="http://warcraft-armory.rubyforge.org/rdoc/">docs</a> or just install the gem:
|
||||
|
||||
<pre lang="sh">sudo gem install warcraft-armory</pre>
|
||||
```sh
|
||||
sudo gem install warcraft-armory
|
||||
```
|
||||
|
||||
<pre lang="ruby">require 'warcraft-armory'
|
||||
```ruby
|
||||
require 'warcraft-armory'
|
||||
adries = WarcraftArmory::Character.find(:eu, :aszune, :adries)
|
||||
adries.description
|
||||
# => "Level 48 Human Warrior"</pre>
|
||||
# => "Level 48 Human Warrior"
|
||||
```
|
||||
|
||||
It's my first gem, so useful comments are appreciated! <a href="http://ariejan.net/tags/warcraft-armory">Keep an eye out for updates</a>!
|
||||
|
||||
|
|
|
@ -7,11 +7,13 @@ slug = "how-to-create-a-dsa-openssl-certificate"
|
|||
|
||||
I just needed an OpenSSL DSA public key. This is not really difficult, you just need to know the right commands. On my Mac I ran the following commands to obtain both private <code>dsa_priv.pem</code> and public <code>dsa_pub.pem</code> keys.
|
||||
|
||||
<pre lang="sh">
|
||||
```sh
|
||||
|
||||
openssl dsaparam 1024 < /dev/random > dsaparam.pem
|
||||
openssl gendsa dsaparam.pem -out dsa_priv.pem
|
||||
openssl dsa -in dsa_priv.pem -pubout -out dsa_pub.pem
|
||||
</pre>
|
||||
|
||||
```
|
||||
|
||||
Needless to say, keep your private key in secure location and make sure you have a backup of it!
|
||||
|
||||
|
|
|
@ -11,10 +11,12 @@ This can be annoying, so you'll need to clean up old indices and other cruft tha
|
|||
|
||||
The solution is über-easy:
|
||||
|
||||
<pre lang="sh">$ sqlite3 mystuff.db
|
||||
```sh
|
||||
$ sqlite3 mystuff.db
|
||||
SQLite version 3.6.6.2
|
||||
sqlite> VACUUM;
|
||||
sqlite> .quit</pre>
|
||||
sqlite> .quit
|
||||
```
|
||||
|
||||
Note that you need to use capitals here. This will clean your db file and reduce its file size dramatically (depending on the amount of cruft you have, of course).
|
||||
|
||||
|
|
|
@ -9,17 +9,21 @@ It's quite easy. Make sure you have RubyGems and Ruby installed first, of course
|
|||
|
||||
The problem:
|
||||
|
||||
<pre lang="sh">$ sudo gem install hpricot
|
||||
```sh
|
||||
$ sudo gem install hpricot
|
||||
Building native extensions. This could take a while...
|
||||
ERROR: Error installing hpricot:
|
||||
ERROR: Failed to build gem native extension.
|
||||
|
||||
/usr/bin/ruby1.8 extconf.rb
|
||||
extconf.rb:1:in `require': no such file to load -- mkmf (LoadError)
|
||||
from extconf.rb:1</pre>
|
||||
from extconf.rb:1
|
||||
```
|
||||
|
||||
The solution:
|
||||
|
||||
<pre lang="sh">sudo apt-get install ruby1.8-dev build-essential
|
||||
sudo gem install hpricot</pre>
|
||||
```sh
|
||||
sudo apt-get install ruby1.8-dev build-essential
|
||||
sudo gem install hpricot
|
||||
```
|
||||
|
||||
|
|
|
@ -7,29 +7,35 @@ slug = "has_one-find-all-that-have-no-associated-object"
|
|||
|
||||
Let me pose a typical Rails situation:
|
||||
|
||||
<pre lang="ruby">class Person < ActiveRecord::Base
|
||||
```ruby
|
||||
class Person < ActiveRecord::Base
|
||||
has_one :fancy_hat
|
||||
end
|
||||
|
||||
class FancyHat < ActiveRecord::Base
|
||||
belongs_to :person
|
||||
end</pre>
|
||||
end
|
||||
```
|
||||
|
||||
Now, how can you get all the people that don't have a fancy hat?
|
||||
|
||||
<pre lang="ruby">class Person < ActiveRecord::Base
|
||||
```ruby
|
||||
class Person < ActiveRecord::Base
|
||||
has_one :fancy_hat
|
||||
|
||||
named_scope :hatless, :joins => 'LEFT JOIN fancy_hats ON fancy_hats.person_id = people.id', :conditions => 'fancy_hats.person_id IS NULL'
|
||||
end</pre>
|
||||
end
|
||||
```
|
||||
|
||||
Now you can find all the hatless people you want.
|
||||
|
||||
FYI: Finding fancy hats that have no one to wear them is a lot easier, because the foreign key is stored in the <code>fancy_hats</code> table.
|
||||
|
||||
<pre lang="ruby">class FancyHat < ActiveRecord::Base
|
||||
```ruby
|
||||
class FancyHat < ActiveRecord::Base
|
||||
belongs_to :person
|
||||
|
||||
named_scope :wearerless, :conditions => { :person_id => nil }
|
||||
end</pre>
|
||||
end
|
||||
```
|
||||
|
||||
|
|
|
@ -13,7 +13,8 @@ Let me show you:
|
|||
|
||||
<strong>Search IMDB</strong>
|
||||
|
||||
<pre lang="sh">$ imdb Star Trek
|
||||
```sh
|
||||
$ imdb Star Trek
|
||||
>> Searching for "Star Trek"
|
||||
> 0060028 | Star Trek (1966) (TV series)
|
||||
> 0796366 | Star Trek (2009)
|
||||
|
@ -24,7 +25,8 @@ Let me show you:
|
|||
> 0084726 | Star Trek: The Wrath of Khan (1982)
|
||||
> 0092007 | Star Trek IV: The Voyage Home (1986)
|
||||
> 0079945 | Star Trek: The Motion Picture (1979)
|
||||
> 0244365 | Enterprise (2001) (TV series)</pre>
|
||||
> 0244365 | Enterprise (2001) (TV series)
|
||||
```
|
||||
|
||||
For clarity, only the ten first search results are shown. I'm thinking of including an option to set the number of returned titles. Let me know what you think about that.
|
||||
|
||||
|
@ -32,7 +34,8 @@ For clarity, only the ten first search results are shown. I'm thinking of includ
|
|||
|
||||
So, let's pick a movie we want to get details about.
|
||||
|
||||
<pre lang="sh">$ imdb 0796366
|
||||
```sh
|
||||
$ imdb 0796366
|
||||
>> Fetching movie 0796366
|
||||
|
||||
Star Trek (2009)
|
||||
|
@ -43,14 +46,17 @@ Directed by: J.J. Abrams
|
|||
Cast: Chris Pine, Zachary Quinto, Leonard Nimoy, Eric Bana, Bruce Greenwood
|
||||
Genre: Action, Adventure, Sci-Fi
|
||||
A chronicle of the early days of James T. Kirk and his fellow USS Enterprise crew members. |
|
||||
========================================================================</pre>
|
||||
========================================================================
|
||||
```
|
||||
|
||||
<strong>Combine with GNU commands</strong>
|
||||
|
||||
Or, as mentioned earlier, you can combine it with other console commands. So to get the rating of a movie, do this:
|
||||
|
||||
<pre lang="sh">$ imdb 0796366 | grep "^Rating" | cut -d" " -f 2
|
||||
8.4</pre>
|
||||
```sh
|
||||
$ imdb 0796366 | grep "^Rating" | cut -d" " -f 2
|
||||
8.4
|
||||
```
|
||||
|
||||
<strong>Installation, source and issues</strong>
|
||||
|
||||
|
@ -58,7 +64,9 @@ I'm still working on perfecting the console output, so if you have any tips, ple
|
|||
|
||||
You can get this as a Ruby Gem with
|
||||
|
||||
<pre lang="sh">sudo gem install imdb</pre>
|
||||
```sh
|
||||
sudo gem install imdb
|
||||
```
|
||||
|
||||
The source is also available at <a href="http://github.com/ariejan/imdb">http://github.com/ariejan/imdb</a>.
|
||||
|
||||
|
|
|
@ -13,31 +13,41 @@ But, how do you use Git effectively for development on a daily basis? Let me exp
|
|||
|
||||
With git you normally have a 'master' branch. This is also the branch you use to sync your code with other repositories. That is also the reason why you should never code in the 'master' branch. Always create a new branch and develop your code there.
|
||||
|
||||
<pre lang="sh">$ git checkout -b new_feature
|
||||
# add, commit, repeat</pre>
|
||||
```sh
|
||||
$ git checkout -b new_feature
|
||||
# add, commit, repeat
|
||||
```
|
||||
|
||||
<strong>Rebase</strong>
|
||||
|
||||
Now, while you are working hard on your new feature, other developers complete theirs and push their changes to the remote master branch. When you're done with your project, you need to first get the most recent version of the project's code.
|
||||
|
||||
<pre lang="sh">$ git checkout master
|
||||
$ git pull</pre>
|
||||
```sh
|
||||
$ git checkout master
|
||||
$ git pull
|
||||
```
|
||||
|
||||
Now, to make merging your new feature easy, you should rebase your new_feature_branch. What this does is add all the commits you just pulled in to your new_feature branch. Any conflicts that arise will happen in your new_feature branch as well, leaving your master branch clean and in order.
|
||||
|
||||
<pre lang="sh">$ git checkout new_feature
|
||||
$ git rebase master</pre>
|
||||
```sh
|
||||
$ git checkout new_feature
|
||||
$ git rebase master
|
||||
```
|
||||
|
||||
<strong>Merge</strong>
|
||||
|
||||
Now, you have resolved all (if any) conflicts with the current code and your new_feature, you can now merge your new_feature into the project's master branch without any problems.
|
||||
|
||||
<pre lang="sh">$ git checkout master
|
||||
$ git merge new_feature</pre>
|
||||
```sh
|
||||
$ git checkout master
|
||||
$ git merge new_feature
|
||||
```
|
||||
|
||||
This will create a new commit, containing your new_feature. Now is also the time to push your changes to the remote repository.
|
||||
|
||||
<pre lang="sh">$ git push origin master</pre>
|
||||
```sh
|
||||
$ git push origin master
|
||||
```
|
||||
|
||||
<strong>What's next?</strong>
|
||||
|
||||
|
|
|
@ -18,8 +18,12 @@ I just released version 0.4.0 of the IMDB Ruby Gem into the wild. There are only
|
|||
|
||||
<strong>Installation or upgrade</strong>
|
||||
|
||||
<pre lang="sh">$ sudo gem install imdb</pre>
|
||||
<pre lang="sh">$ sudo gem update imdb</pre>
|
||||
```sh
|
||||
$ sudo gem install imdb
|
||||
```
|
||||
```sh
|
||||
$ sudo gem update imdb
|
||||
```
|
||||
|
||||
<strong>Issues, source or contributions</strong>
|
||||
|
||||
|
|
|
@ -9,24 +9,28 @@ Yesterday I ran into a little problem running <a href="http://slick.cokeandcode.
|
|||
|
||||
If you're a user of LWJGL, you'll be using JInput as well. Unfortunately, JInput does not currently have any 64 bit native libraries as it is only providing for PPC and i386 (32bit). This is a problem because Mac OS X users are somewhat bound to Java 6 64bit. There is no 32 bit version of Java 6 for Mac OS X.
|
||||
<!--more-->
|
||||
<pre lang="none">
|
||||
```none
|
||||
|
||||
$ file libjinput-osx.jnilib
|
||||
libjinput-osx.jnilib: Mach-O universal binary with 2 architectures
|
||||
libjinput-osx.jnilib (for architecture ppc): Mach-O dynamically linked shared library ppc
|
||||
libjinput-osx.jnilib (for architecture i386): Mach-O dynamically linked shared library i386
|
||||
</pre>
|
||||
|
||||
```
|
||||
|
||||
Thus, the default JInput natives provided by LWJGL, and subsequently by projects like <a href="http://slick.cokeandcode.com/index.php">Slick 2D</a> and <a href="http://www.jmonkeyengine.com/">jME</a> are of no use on a system running Java 6 64 bit. Also games and other apps based on those libraries won't run on Java 6 64 bit!
|
||||
|
||||
To solve this, I recompiled the JInput natives from CVS with 64 bit support:
|
||||
|
||||
<pre lang="none">
|
||||
```none
|
||||
|
||||
$ file libjinput-osx.jnilib
|
||||
libjinput-osx.jnilib: Mach-O universal binary with 3 architectures
|
||||
libjinput-osx.jnilib (for architecture ppc): Mach-O dynamically linked shared library ppc
|
||||
libjinput-osx.jnilib (for architecture i386): Mach-O dynamically linked shared library i386
|
||||
libjinput-osx.jnilib (for architecture x86_64): Mach-O 64-bit dynamically linked shared library x86_64
|
||||
</pre>
|
||||
|
||||
```
|
||||
|
||||
I packaged the Mac OS X native (<code>libjinput-osx.jnilib</code>) and the resulting <code>jinput.jar</code> and <code>jinput-test.jar</code> up and you can download them here:
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user