I just started thinking about a project and am seeking some help
regarding its design.
The idea would be to create labels (i.e. address labels).I could let
users type the text manually, but I dont think they would appreciate
that when they'll want to print 5000 addresses.
I can also probably let them import csv files, that should not be a
problem, but playing with those files is not extremely user friendly
either. Besides, I dont want them to get bored after 5 minutes and
going back to their good old excel after 5 minutes.
The most "exciting" feature i thought of is also the one that scares
me the most when it comes to coding:
I'd love to let users actually connect to any database they want,
retrieve a list of tables and fields and let them select anything they
want on the labels.
I can already see the tables with their fields that they could drag
and drop on a label template... Well, I'm dreaming, and I'd love to
know if this is going to stay just a dream or if this is actually
possible using Ruby and Rails...
Summary : Is there any way I can let users select (from the
application) a database they want to use? Or is the database.yml file
the only way of connecting to a database?
Actually, it's not too hard IMHO. You certainly don't need a
different URL / app instance for each database. In fact, you can pick
a database and run the pending migrations from a before filter in just
a few lines of code. Here's a demo:
g@bang:~/tmpapp$ cat db/migrate/001_create_initial_tables.rb
class CreateInitialTables < ActiveRecord::Migration
def self.up
create_table :things do |t|
t.column :name, :string
end
end
def self.down
drop_table :things
end
end
g@bang:~/tmpapp$ cat app/models/thing.rb
class Thing < ActiveRecord::Base
end
g@bang:~/tmpapp$ cat app/controllers/things_controller.rb
require 'ostruct'
class ThingsController < ApplicationController
before_filter :connect_to_database
def connect_to_database
config = session[:database] ||= ActiveRecord::Base.configurations[RAILS_ENV]
ActiveRecord::Base.establish_connection(config)
ActiveRecord::Migrator.migrate("db/migrate")
@database = OpenStruct.new(config)
end
def set_database
session[:database] = params[:database]
redirect_to :action => 'list'
end
def list
@things = Thing.find(:all)
@thing = Thing.new
end