github.com/sbsoftware/crumble-orma

Integration shard for `crumble` and `orma`

2 stars
2 dependents
License: MIT

Installation

# Add this to your shard.yml
dependencies:
  crumble-orma:
    github: sbsoftware/crumble-orma
    version: ~> 0.6.0

Then run:

shards install

shard.yml

Crystal
>= 1.16.3
License
MIT
Author
Stefan Bilharz

Dependencies

Runtime Dependencies

  • orma*github: sbsoftware/orma
  • crumble>= 0.34.1github: sbsoftware/crumble

Development Dependencies

  • sqlite3*github: crystal-lang/crystal-sqlite3dev

README

crumble-orma

Small integration helpers between Crumble and Orma.

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      crumble-orma:
        github: sbsoftware/crumble-orma
  2. Run shards install

Usage

require "crumble-orma"

Page model loading

class User < Orma::Record
  column name : String
end

class UserPage < Crumble::Page
  model user : User

  template do
    p { user.name }
  end
end

The model macro defines a user_id path param, loads the record before handling the request, exposes user directly in the page template, and returns 404 when the record is missing. With crumble >= 0.31.0, prefer defining the page markup directly with template do.

You can provide a redirect or a fallback view when the record is missing:

class UserPage < Crumble::Page
  model user : User, fallback_redirect: "/"

  template do
    p { user.name }
  end
end

class MissingUserView
  include Crumble::ContextView

  template do
    p { "User not found" }
  end
end

class UserFallbackViewPage < Crumble::Page
  model user : User, fallback_view: MissingUserView

  template do
    p { user.name }
  end
end

Orma attributes in HTML + CSS

class Model < Orma::Record
  column active : Bool
end

class View
  getter model : Model
  def initialize(@model); end

  ToHtml.instance_template do
    div model.active do
      model.active ? "Active" : "Inactive"
    end
  end
end

class Style < CSS::Stylesheet
  rule Model.active(false) do
    color "#8a8a8a"
  end
end

Rendering adds a data-orma-... attribute to the element, and the attribute can be used in CSS selectors via Model.active(false).

Development

  • Install dependencies: shards install
  • Run tests: crystal spec
  • Format code before PRs: crystal tool format

Contributing

  1. Fork it (https://github.com/sbsoftware/crumble-orma/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

Contributors