Git Product home page Git Product logo

sinatra-admin's Introduction

SinatraAdmin Build Status

Sinatra application that allows us to have an admin dashboard with minimal effort.

Installation

Add this line to your application's Gemfile:

gem 'sinatra-admin'

And then execute:

$ bundle

Or install it yourself as:

$ gem install sinatra-admin

Usage

By default SinatraAdmin assumes that it's mounted over an "admin" namespace. This is how SinatraAdmin should be configured:

  1. Set ENV['SINATRA_ADMIN_SECRET']. This secret key is used by Rack::Session::Cookie to encrypt your cookies. Warden needs this session secret key to keep the user sessions. If you do not set this value you're going to see a security warning.

    $ export SINATRA_ADMIN_SECRET="my_secret_key"
  2. Define your project namespace and your Sinatra applications.

    module MyApp
      class API < Sinatra::Base
        #Config for API here
      end
    
      class Admin < Sinatra::Base
        #Config for Admin here
      end
    end
  3. Add SinatraAdmin::App middleware to your admin application.

    class MyApp::Admin
      use SinatraAdmin::App
    end
  4. Register model resources(let's assume that we have User and Comment models). It creates the 7 REST actions to /create/update/remove records.

    class MyApp::Admin
      SinatraAdmin.register 'User'
      SinatraAdmin.register 'Comment'
    end
  5. Define your root resource(optional). This is going to be the first page where the application is going to redirect you after the login. SinatraAdmin defines the first registered resource as the default root. In this case it will get 'User'(according to point number 3). If you want, you can set a different resource as the default one.

    class MyApp::Admin
      SinatraAdmin.root 'Comment'
    end
  6. Define your custom resources. Having model resources sometimes is not enough and we might want to see some stats about our application. An example could be: "As an admin I want to see how many user accounts has been registered". Let's take a look at how to define custom resources.

    class MyApp::Admin
      SinatraAdmin.register 'Stats' do
        get '/?' do
          @message = 'Welcome to SinatraAdmin custom pages!'
          @accounts_counter = User.count
          haml 'stats/index'.to_sym
        end
      end
    end

    If you try to access that custom page you are going to see an exception saying something like: "Errno::ENOENT at /admin/stats No such file or directory - /path/to/the/gem/sinatra-admin/views/stats/index.haml" It's because SinatraAdmin tries to find the template in the views folder of sinatra-admin. Obviously, that custom template does not exist in the gem. SinatraAdmin has a method to extend the views path and it allows us to find the template that we are looking for. This takes us to the next point.

  7. Extend your views path(Only for custom resources). SinatraAdmin has the method :extend_views_from. This method receives a value that should either be a String instance with the path to views folder or be a Sinatra application. SinatraAdmin expects to be mounted over an "admin" namespace, that's why it's going to look the view in: the/extented/path/admin

    class MyApp::Admin
      SinatraAdmin.extend_views_from(MyApp::API) #It'll look at path/to/my_app/api/views/admin/stats/index.haml
      SinatraAdmin.extend_views_from('path/to/views/folder') #It'll look at path/to/views/folder/admin/stats/index.haml
    end
  8. Wrapping it up.

    class MyApp::Admin < Sinatra::Base
      use SinatraAmin::App
      SinatraAdmin.root 'Post'
      SinatraAdmin.resource 'User'
      SinatraAdmin.resource 'Comment'
      SinatraAdmin.resource 'Post'
      SinatraAdmin.register 'Stats' do
        get '/?' do
          @message = 'Welcome to SinatraAdmin custom pages!'
          @accounts_counter = User.count
          haml 'stats/index'.to_sym
        end
      end
      SinatraAdmin.extend_views_from(MyApp::API)
    end
  9. Run it. We assume you're using a config.ru file to run your Sinatra application. This is how it should look like:

    require 'path/to/my_app/api'
    require 'path/to/my_app/admin'
    
    map "/api"
      run MyApp::API
    end
    
    map "/admin" do #mounted over "/admin" namespace(mandatory)
      run MyApp::Admin
    end

Constraints

  • SinatraAdmin only works if you mount it over "admin" namespace like in the example(Point 8).

  • Even when you pass a block with get/post/put/patch/delete when you register a model resource(like User), SinatraAdmin does not have a way to access them(only the URL). This is a TODO feature.

  • Even when you pass a block with post/put/patch/delete when you register a custom resource(Like MyStats), SinatraAdmin does not have a way to access them(only the URL). This is a TODO feature.

Notes

  • SinatraAdmin uses Mongoid by default. TODO: Add activeRecord support.

  • SinatraAdmin uses Warden for authentication.

  • SinatraAdmin uses Bootstrap(2.3.2) and Jquery(2.1.1).

  • SinatraAdmin uses Bootstrap Datepicker. More info http://www.eyecon.ro/bootstrap-datepicker/

  • SinatraAdmin comes with an Admin model by default. The constant is SinatraAdmin::Admin. It has :first_name, :last_name, :email and :password fields. Password is encrypted and stored in :password_hash field. :email and :password are required fields and :email should have a correct format.

  • You can contribute to this Project. Contributing means not only adding features but also writing documentation, adding issues, refactoring code or just sending us either a <3 if you liked the project or a </3 if you did not like it ;)

  • Current version: 0.1.2

Contributing

  1. Fork it ( https://github.com/[my-github-username]/sinatra-admin/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

sinatra-admin's People

Contributors

cjcaj avatar davidlks avatar franciscodelgadodev avatar jwallaceparker avatar kennycyb avatar nurulazradb avatar vahakmatavosian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sinatra-admin's Issues

Index page. Filters.

Based on the last open issue #24 and following the new DSL convention(suggested in the mentioned issue). We'd like to add filters to the index page. As per @kennycyb suggestion, having filters below the table headers would be nice(this is a link for an example https://demos.devexpress.com/ASPxGridViewDemos/Filtering/FilterRow.aspx). Let's build something similar with bootstrap.

This issue is to add filters for index pages. My DSL suggestion would be this:

class MyApp < Sinatra::Base
  use SinatraAdmin::App do |config|
    config.root "Home"
    config.register "Post" #=> Single resource registration. No config.
    config.register "User" do |users|  #=> Single resource registration. Config added.
      users.index do
        ### > Added by the last issue ###
        sort_by :created_at, :desc #=> Default sort is by id?? 
        columns do
          add :email #=> Title by default is underscored. Content by default is the database value
          add :full_name, content: ->(user){user.firstname + user.lastname} } #=> Can pass a lambda with dynamic logic          
          add :username, title: "nickname" #=> Can specify a custom title
        end
        ### > Added by this issue ###
        filters do #=> Filters will be a form at the left of the listing.
          add :username #=> Will display a text input by default
          add :active, type: select, values: [true, false] #=> It'd be great if we can do this, like adding filter type :) Not necessary right now. Text would be enough to start.
        end
      end
    end
    config.register 'Home' do #=> Registration of custom resource. It wont change so far :)
      get '/?:home?' do
        @msg = "Welcome to home page huerco!"
        haml :home
      end
    end
  end
end

We're open to any other suggestion.

Thanks,
Francisco.

Index page. Columns customization.

Right now the gem is displaying all columns in the database for each registered resource(model). Sometimes, we have resources with a lot of columns we don't care about(for admin purposes) and we don't want to display those values as part of my resource table(listing). We would like to customise some tables and specify which columns we want to display as well as how the values for those columns are going to be formed. For instance, let's say you register a User resource(pretty common) and you want to customise some columns in the index page.

Having a DSL like this would be great.

class MyApp < Sinatra::Base
  use SinatraAdmin::App do |config|
    config.root "Home"
    config.register "Post" #=> Single resource registration. No config.
    config.register "User" do |users|  #=> Single resource registration. Config added.
      users.index do
        sort_by :created_at, :desc #=> Default sort?? 
        columns do
          add :email #=> Title by default is underscored. Content by default is the database value
          add :full_name, content: ->(user){user.firstname + user.lastname} } #=> Content built dynamically.
          add :username, title: "nickname" #=> Can specify a custom title(header)
        end
      end
    end
    config.register 'Home' do #=> Registration of custom resource. It wont change so far :)
      get '/?:home?' do
        @msg = "Welcome to home page huerco!"
        haml :home
      end
    end
  end
end

We're open to any other suggestion.

Thanks,
Francisco.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.