Amatista Build Status docrystal.org

Deprecated!.

you want to use Kemal instead

This is a web framework build in Crystal to create quick applications.

Shard file

shard.yml

name: myapp
version: 0.0.1

dependencies:
  amatista:
    github: werner/amatista

Basic Usage

require "amatista"

class HelloWorldController  Amatista::Controller
  get "/" do
    html = %(<h1> Hello World </h1>)
    respond_to(:html, html)
  end
end

class Main  Amatista::Base
  configure do |conf|
    conf[:secret_key] = "secret"
  end
end

app = Main.new

app.run 3000

Callbacks


class ApplicationController  Amatista::Controller
  #It will be a redirection if the condition is fulfilled,
  #it should not be a session with a key user_id for the redirect to works
  before_filter(condition: -> { !get_session("user_id") }) do
    redirect_to("/sessions/new")
  end
end

View System


class HelloWorldController  Amatista::Controller
  get "/tasks" do
    tasks = Task.all
    # You're going to need a LayoutView class as 
    # a layout for set_view method to work
    respond_to(:html, IndexView.new(tasks).set_view)
  end
  
  get "/tasks.json" do
    tasks = Task.all
    respond_to(:json, tasks.to_s.to_json)
  end
end

class LayoutView  Amatista::BaseView
  def initialize(@include)
  end

  set_ecr "layout"
end

class IndexView  Amatista::BaseView
  def initialize(@tasks)
  end

  def tasks_count
    @tasks.count
  end

  set_ecr "index"
end

#Views: 
#layout.ecr
html>
  head>
    title>Todo App/title>
    <link rel="stylesheet" type="text/css" href="/app/assets/stylesheets/bootstrap-theme.min.css" media="all">
    <link rel="stylesheet" type="text/css" href="/app/assets/stylesheets/bootstrap.min.css" media="all">
  </head>
  body>
    div class="container">
      div class="row">
        div class="col-xs-8">
          %= @include %>
        /div>
      </div>
    /div>
    <script src="/app/assets/javascripts/jquery-2.1.3.min.js"></script>
    script src="/app/assets/javascripts/main.js">/script>
    <script src="/app/assets/javascripts/bootstrap.min.js"></script>
  /body>
</html>

#index.ecr
div class="panel panel-success">
  div class="panel-heading">
    h2 class="panel-title">Todo Tasks/h2>
  </div>

  div class="panel-body">
    table class="table">
      tbody>
      % @tasks.each do |task| %>
        tr>
          td>
            %= check_box_tag(:task, "id#{task[0]}", task[0], task[2], { class: "checkTask" }) %>
            %= label_tag("task_id#{task[0]}", task[1].to_s) %>
          /td>
          <td>
            <%= link_to("Edit", "/tasks/edit/#{task[0]}", { class: "btn btn-success btn-xs" }) %>
          /td>
          <td>
            <%= link_to("Delete", "/tasks/delete/#{task[0]}", { class: "del btn btn-danger btn-xs" }) %>
          /td>
        <tr>
      <% end %>
      <tbody>
    </table>
    %= link_to("New Task", "/tasks/new", { class: "btn btn-info btn-xs" } ) %>
    %= label_tag("total", "Total: #{tasks_count}" ) %>
  /div>
</div>
Example

Contributing

  1. Fork it ( https://github.com/werner/amatista/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