Sha256: 78b2ab7165c84412c51ac48c717ceb201cf647d74408c64eda68ecf86d9ab433

Contents?: true

Size: 1.59 KB

Versions: 4

Compression:

Stored size: 1.59 KB

Contents

module Sinatra
  module SequelOrmGen

    SEQUEL = <<-SEQUEL
module SequelInitializer
  def self.registered(app)
    Sequel::Model.plugin(:schema)
    app.configure(:development) { Sequel.connect('your_dev_db_here') }
    app.configure(:production)  { Sequel.connect('your_production_db_here') }
    app.configure(:test)        { Sequel.connect('your_test_db_here') }
  end
end
SEQUEL

    USER = <<-USER
    class User < Sequel::Model(:users)
      unless table_exists?
        set_schema do
          primary_key :id
          string :name
          string :username
          string :email
          string :crypted_password
        end
        create_table
      end

      attr_accessor :password, :password_confirmation

      def self.authenticate(username, password)
        user = User.filter(:username => username).first
        user && user.has_password?(password) ? user : nil
      end

      def encrypt_password
        return if password.blank?
        self.crypted_password = BCrypt::Password.create(password)
      end

      def has_password?(password)
        BCrypt::Password.new(crypted_password) == password
      end

      def before_save
        encrypt_password
      end

      def validate
        errors.add :password, "must not be empty" if self.crypted_password.blank? && password.blank?
        errors.add :password, "must match password confirmation" unless password == password_confirmation
      end
    end
USER

    def setup_orm
      require_dependencies 'sequel'
      create_file("config/initializers/sequel.rb", SEQUEL)
      create_file("app/models/user.rb", USER)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
darkhelmet-sinatra_more-0.3.36 generators/components/orms/sequel_orm_gen.rb
darkhelmet-sinatra_more-0.3.35 generators/components/orms/sequel_orm_gen.rb
darkhelmet-sinatra_more-0.3.34 generators/components/orms/sequel_orm_gen.rb
darkhelmet-sinatra_more-0.3.33 generators/components/orms/sequel_orm_gen.rb