Skip to content

sinatra extension to display records from tables in a quick way

License

Notifications You must be signed in to change notification settings

shovanj/sinatra-scaffold

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple sinatra extension that can be used to create a quick admin interface to access table contents. Uses activerecord to fetch table info and data.

Usage:

  1. Require your AR model files in your app. eg:
Dir["#{File.dirname(__FILE__)}/models/*.rb"].sort!.each {|f| require_relative f}
  1. Register the extension in your sinatra app. Add will_paginate extension for pagination.
register Sinatra::Scaffold
register WillPaginate::Sinatra
  1. Set database connection inside the sinatra app.
ActiveRecord::Base.
  establish_connection(
  :adapter  => "mysql2",
  :database => "scaffold_dev",
  :username => 'root',
  :password => '',
  :host => 'localhost'
  )
)

3 Specify tables and respective columns that you want to display.

# following will generate /users url that will display the records in a table.
# to use search functionality simply add columns that you want to be searchable.
scaffold :users do |config|
  config.hide :password_digest, :security_answer
  config.search :name, :email, :login
end

# following will only display colums that are specified
scaffold :items do |config|
  config.only :name, :email
end
scaffold(:users) {|c| c.only([:first_name, :last_name])}

Sample App:

  require 'active_record'
  Dir["#{File.dirname(__FILE__)}/models/*.rb"].sort!.each {|f| require_relative f}

  ActiveRecord::Base.establish_connection(
    :adapter  => "mysql2",
    :database => "scaffold_dev",
    :username => 'root',
    :password => '',
    :host => 'localhost'
  )
                                                                                                                                            
 class ScaffoldApp < Sinatra::Base
    register Sinatra::Scaffold
    register WillPaginate::Sinatra
    
    ScaffoldApp.database_connection = ActiveRecord::Base.connection
    
    scaffold(:all) { |config|
      config.hide([:lock_version, :created_at, :bench_marked_at, :id])
    }
    
    scaffold(:users) {|c| c.only([:first_name, :last_name])}
 end

About

sinatra extension to display records from tables in a quick way

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published