github.com/sbsoftware/crumble-orma

Integration shard for `crumble` and `orma`

2 stars
2 dependents
License: MIT

Nothing has been indexed for 0.3.1 yet. The tag is recorded, its shard.yml has not been read, so the manifest and dependency list below are empty because they are unknown rather than because they are absent.

Installation

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

Then run:

shards install

shard.yml

No shard.yml has been indexed for 0.3.1. You can read it on the repository.

Dependencies

Unknown: the shard.yml for this version has not been read yet.

README

This README is the one indexed from the repository at its latest ref, not from the tag for this version.

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