bootstrap snippets

Bootstraping your database with rake

Tagged bootstrap, ruby, rails, rake  Languages ruby

In lib/tasks/app.rake:

namespace :db do
  desc "Bootstraps the database"
  task :bootstrap => ['db:schema:load'] do

   { 'Ruby' => 'ruby',
      'JavaScript' => 'javascript',
    }.each do |name, short_name|
      Category.create!(:name => name, :short_name => short_name)


A Reusable JavaScript Modal Dialog Based on Bootstrap Modal

Tagged bootstrap, modal, javascript, coffeescript  Languages coffeescript

The Coffeescript class:

root = exports ? this

class Modal
  constructor: (options = {}) ->
    @options = options
    @id = ? 'modal-dialog-' + Math.round((Math.random() * 1000))
    @dom_id = "##{@id}"
    @onPrimaryButton = options.onPrimaryButton ? ->
    @onSecondaryButton = options.onSecondaryButton ? ->
    @template = options.template ? HandlebarsTemplates['modal'](id: @id)

  render: ->
    @modal = $(@dom_id)
    _this = this
    @modal.find('.btn-primary').on 'click', ->
    @modal.find('.btn-secondary').on 'click', ->

  setButtonText: (primary, secondary) ->

  setTitle: (title) ->

  setBody: (body) ->

  show: ->

  hide: ->

root.Modal = Modal

The view (bootstrap 2.3.2):

<div class="modal hide fade" id="{{id}}">
  <div class="modal-header">
    <button aria-hidden="true" class="close" data-dismiss="modal" type="button"> &times;</button>
    <h3 class="title"></h3>
  <div class="modal-body body">
  <div class="modal-footer">
    <button class="btn btn-secondary" data-dismiss="modal">Cancel</button>
    <button class="btn btn-primary" data-dismiss="modal">Ok</button>

Usage, e.g.:

class Notifications
  constructor:  ->
    @modal = new Modal
      onPrimaryButton: $.proxy(@onItemDelete, this)
      onSecondaryButton: $.proxy(@onItemRead, this)
  onItemDelete: ->
    # do something when primary button was clicked
  onItemRead: ->
    # do something when secondary button was clicked