Sha256: 4e6d64a9f942db7db2d82d79720774ac86e4b3193c3e0b3c12b7f6e6a0bb7184

Contents?: true

Size: 1.69 KB

Versions: 4

Compression:

Stored size: 1.69 KB

Contents

module SinatraMore
  module ActiverecordOrmGen
    
    AR = <<-AR
module ActiveRecordInitializer
  def self.registered(app)
    app.configure do
      ActiveRecord::Base.establish_connection(
        :adapter => 'sqlite3',
        :database => 'your_db_here'
      )
    end
  end
end
AR

RAKE = <<-RAKE
require 'active_record'
require 'sinatra/base'

namespace :db do
  desc "Migrate the database"
  task(:migrate) do
    load 'config/boot.rb'
    ActiveRecord::Base.logger = Logger.new(STDOUT)
    ActiveRecord::Migration.verbose = true
    ActiveRecord::Migrator.migrate("db/migrate")
  end
end
RAKE


   MIGRATION = <<-MIGRATION
class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
       t.column :name, :string
       t.column :username, :string
       t.column :email, :string
       t.column :crypted_password, :string
       t.column :created_at, :datetime
    end
  end

  def self.down
    drop_table :users
  end
end
MIGRATION

  USER = <<-USER
class User < ActiveRecord::Base  
  def self.authenticate(username, password)
    user = User.first(:conditions => { :username => username })
    user && user.has_password?(password) ? user : nil
  end
  
  def encrypt_password
    self.crypted_password = BCrypt::Password.create(password)
  end
  
  def has_password?(password)
    BCrypt::Password.new(crypted_password) == password
  end
end
USER
    
    def setup_orm
      insert_require 'active_record', :path => "config/dependencies.rb", :indent => 2
      create_file("config/initializers/active_record.rb", AR)
      create_file("Rakefile", RAKE)
      create_file("db/migrate/001_create_users.rb", MIGRATION)
      create_file("app/models/user.rb", USER)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sinatra_more-0.3.6 generators/components/orms/activerecord_orm_gen.rb
sinatra_more-0.3.5 generators/components/orms/activerecord_orm_gen.rb
sinatra_more-0.3.4 generators/components/orms/activerecord_orm_gen.rb
sinatra_more-0.3.3 generators/components/orms/activerecord_orm_gen.rb