Admin Plugin for Your Rails App – admin_data FTW

Ruby on Rails No Comments »

After installing, trying and discarding typus, casein as well as ActiveScaffold, I ended up with one of the best plugins I’ve seen, and certainly the best for utterly effortless admin panel setup,  admin_data.

It is

  • super-quick to install (Rails 3 is the gem, Rails 2 is a plugin branch, no digging through trees on github),
  • unintrusive (all code is in vendor/admin_data where it belongs),
  • requires no set-up,
  • correctly (!) gets all model information from your model definitions (primary_key, foreign_key, relationships etc.),
  • including multiple databases, SQL Server connections via activerecord-sqlserver-adapter, and even
  • composite primary keys,
  • making it great for legacy data,
  • uses your existing authentication solution.

It’s basically completely abstracted sitting on the top of ActiveRecord, and this design choice is very powerful. Not only that, it even checks your model definitions along the way. If your table is visible and editable in admin_data, it’s probably defined correctly (especially with regards to foreign_key, primary_key(s), set_table_name and other stuff relevant for legacy data). And did I mention the configuration is one block in one file in your app?

Have a look at the readme whose four lines are indicative of the DRYness and brilliance of this plugin.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Testing AuthLogic with RSpec and Mock Helpers

AuthLogic, RSpec, Ruby on Rails No Comments »

Note: This all concerns Rails 2.3.x and recent versions of AuthLogic

Testing AuthLogic with RSpec is still a puzzle with several parts that can be difficult to fit together. Very helpful is a sample project on github that demonstrates several testing options with RSpec.

A series of mock helper methods that you can stuff in your spec_helper.rb can ease testing, letting you call

login

or

current_user

anywhere in your specs.

If you get errors such as

Mock "UserSession_1002" received unexpected message :record with (no args)

when mocking, the issue might be that current_user_session.record (or the underlying method which is hard to find) which is likely called in your ApplicationController is deprecated in favour of current_user_session.user

This deprecated code is still in the authlogic sample project as well as Ryan Bates’ Railscast 160, so it might be confusing. Alternatively you can mock the :record call as well as some have accomplished as posted in the comments of the original post.

 

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Install Gems Remotely When Using Git and Gitosis

Capistrano, Git, Ruby, Ruby on Rails No Comments »

To host your private gems you have several options. The cheapest and most scalable solution will likely be to run your own gitosis server. Then you build your gems as described in this railscast and push them up to your gitosis repo. But how to deploy and install these gems when the gem is updated?

Installing a gem from Git involves four steps:

  1. Clone the Git repository.
  2. Change to the new directory.
  3. Build the gem.
  4. Install the gem.

Git post-receive Hook

One way is to implement a Git post-receive hook. This hook is in the hooks directory of your repo, and it has to be edited on the server. You cannot edit a hook locally and push it to the remote server. So ssh into your remote server and create a hook named post-receive (with 755 permissions) to look as so (Update: because of a “Permission denied – /tmp/stdout.echoe” error rm has to be run with sudo and visudo permissions for the git user has to be extended to ALL):

#! /bin/sh
cd /home/git
unset GIT_DIR
sudo rm -rf mygem
git clone /home/git/repositories/mygem.git
cd mygem && sudo rake manifest && sudo rake build_gemspec && sudo rake install 

Be sure to have the following in your visudo on the remote server:

git ALL=NOPASSWD: ALL

That’s it. Very easy.

Of course you can now setup different remote servers and push to them as needed.

Capistrano

For rails you could use capistrano and run these task only when deploying a Rails project, by putting this into your deploy.rb fild:

namespace :gems do

desc “Install gem”
  task :install_mygem, :roles => :app do
    run “cd /home/git && sudo rm -rf mygem”
    run “git clone git@yourserver.com:mygem.git
    run “cd /home/git/mygem && sudo rake manifest && sudo rake build_gemspec && sudo rake install”
  end

end

Hope it helps. If you have a simpler or more effective way, please post in the comments.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

SQL Server with FreeTDS Unicode Data Error

Ruby on Rails, SQL Server 5 Comments »

Update (Aug. 28, 2011): You may want to check out tiny_tds which obviates many of the below settings and gives you a richer and more scalable connection than ODBC.

If you get

Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier.

when connecting to SQL Server from Linux via FreeTDS, you don’t set the FreeTDS version correctly.

To use SQL Server from Rails, I usually just create shared DSNs in /etc/odbc.ini (Ubuntu) with connection information that looks like so:

[SQLServerLocal]
Description  = FreeTDS
Driver  = FreeTDS
Server  = 192.168.0.1
Trace = No
Database  = my_database
TDS_Version = 8.0
Port  = 1433

Note the syntax: It must be Server, NOT Servername *Update: this only applies to the odbc.ini setup used here, if you need to read client charset = UTF-8 from freetds.conf, it has to be Servername, see update below* and TDS_Version has an underscore.

The standard ODBCConfig visual tool will usually set “Servername” instead of “Server” and this will not work.

The above settings should just work (provided FreeTDS is installed correctly) with the ActiveRecord SQL Server adapter and these settings in database.yml:

development:
adapter: sqlserver
mode: odbc
dsn: SQLServerLocal
database: my_database
username: some_user
password: secret
encoding: utf8

Also be sure to read general instruction on how to get Linux clients talk to SQL Server,  as well as the rails setup and connectivity for different clients.

Update: Turns out I still got errors such as

unixODBC][FreeTDS][SQL Server]Error converting characters into server’s character set. Some character(s) could not be converted

when using the “odbc.ini”-only setup outlined above.

It may be necessary to have the ODBC driver read the

client charset = UTF-8

from the /etc/freetds/freetds.conf file. And it can be set only there, not in the odbc.ini definition.

SO for example if you have this definition in your freetds.conf:

[sqlserver_local]
host = localhost
port = 1433
tds version = 8.0
client charset = UTF-8

the odbc.ini above has to be changed to:

[SQLServerLocal]
Description  = FreeTDS
Driver  = FreeTDS
Servername = sqlserver_local
Trace = No
Database  = my_database
TDS_Version = 8.0
Port  = 1433

Note that Server = has to be changed to Servername =. Hope it helps.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Quickstart with YQL and HTTParty with Rails

Ruby on Rails, YQL 1 Comment »

YQL makes it super-easy to consume data in your web app through a unified SQL-like API. And HTTParty by John Nunemakers makes HTTP really easy. A perfect match.

In environment.rb:

config.gem ‘httparty’

Now, just create a model. Be sure to use the public endpoint for now, as the others need oauth authentication.

class News

  include HTTParty
  base_uri  ‘http://query.yahooapis.com/v1/public/yql’
  
  def self.new_york_news
    self.get(“”, :query => {:q => ‘select title, abstract, url from search.news where query = “%New York%”‘,
        :format => ‘json’

      })
  end

  def self.new_york_news_hash
    self.new_york_news.parsed_response["query"]["results"]["result"]
  end
 
end

In you controller:

class NewsController < ApplicationController

  def index
    @news = News.new_york_news_hash
  end

end

And your view (for example):

<h1>New York News</h1>
<% @news.each do |news_item| %>
  <% content_tag :h2 do %>
    <%= link_to news_item["title"], news_item["url"]  %>
    <% end %>
    <% content_tag :div do  %>
      <% content_tag :p do %>
        <%= news_item["abstract"]  %>
      <% end %>
    <% end %>
  <%  end %>

Be sure to check the API docs of YQL for many other options and watch the excellent screencasts by Christian Heilmann in the YUI theater and elsewhere. The possibilities are endless.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]