Last active
August 29, 2015 14:05
-
-
Save activestylus/36c13c6df411761db605 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'bundler' | |
Bundler.setup(:default, :development) | |
require 'lotus' | |
Lotus::Controller.configure do | |
modules do | |
include Music::Helpers | |
end | |
end | |
mapper = Lotus::Model::Mapper.new do | |
collection :albums do | |
entity Album | |
attribute :title, String | |
end | |
end | |
db_url = 'postgres://host:port/database' | |
adapter = Lotus::Model::Adapters::SqlAdapter.new(mapper, db_url) | |
Music::Repository::Albums.adapter = adapter | |
module Music | |
class App < Lotus::Application | |
configure do | |
routes do | |
resources :albums | |
end | |
end | |
load! | |
end | |
module Model | |
class Album | |
include Lotus::Entity | |
self.attributes = :title, :info | |
end | |
end | |
module Repository | |
class Albums | |
include Lotus::Repository | |
def self.online | |
query do | |
where(online: true).order(:title) | |
end | |
end | |
end | |
end | |
module Controllers | |
module Albums | |
include Music::Controller | |
module AllowedParams | |
params do | |
param :title | |
end | |
end | |
action 'Index' do | |
expose :albums | |
def call(params) | |
@albums = Repository::Albums.online | |
end | |
end | |
action 'Show' do | |
expose :album | |
def call(params) | |
@album = album | |
end | |
end | |
action 'New' do | |
expose :album | |
def call(params) | |
@album = Model::Album.new | |
end | |
end | |
action 'Create' do | |
include AllowedParams | |
def call(params) | |
@album = Model::Album.create params[:album] | |
if @album.valid? | |
redirect_to path(:album, id: @album.id) | |
else | |
Views::Albums::New.new.render | |
end | |
end | |
end | |
action 'Edit' do | |
expose :album | |
def call(params) | |
@album = album | |
end | |
end | |
action 'Update' do | |
include AllowedParams | |
def call(params) | |
@album = Model::Album.update params[:album] | |
if @album.valid? | |
redirect_to album_path(@album) | |
else | |
Views::Albums::Edit.new.render | |
end | |
end | |
end | |
action 'Delete' do | |
def call(params) | |
album.destroy | |
redirect_to albums_path | |
end | |
end | |
protected | |
def album | |
@album = Model::Album.find(params[:id]) | |
end | |
end | |
end | |
module Views | |
module Albums | |
class Index | |
include Albums::View | |
def title | |
"Albums" | |
end | |
end | |
class Show | |
include Albums::View | |
def title | |
album.title | |
end | |
end | |
class New | |
include Albums::View | |
def title | |
"New Album" | |
end | |
end | |
class Edit | |
include Albums::View | |
def title | |
"Edit Album: #{album.title}" | |
end | |
end | |
end | |
end | |
module View | |
include Albums::Helpers | |
end | |
module Helpers | |
def album_path(album) | |
App.path(:album, album.id) | |
end | |
def edit_album_path(album) | |
App.path(:edit_album, album.id) | |
end | |
def albums_path | |
App.path(:albums, album.id) | |
end | |
end | |
end | |
run Music::App.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment