Skip to content

Instantly share code, notes, and snippets.

@robyurkowski
Last active August 29, 2015 14:13
Show Gist options
  • Save robyurkowski/fd560b058587132ec972 to your computer and use it in GitHub Desktop.
Save robyurkowski/fd560b058587132ec972 to your computer and use it in GitHub Desktop.
module Web
class Application < Lotus::Application
configure do
##
# BASIC
#
# Define the root path of this application.
# All the paths specified in this configuration are relative to this one.
#
root __dir__
# Relative load paths where this application will recursively load the code.
# Remember to add directories here when you create new.
#
load_paths << [
'controllers',
'views'
]
# Handle exceptions with HTTP statuses (true) or don't catch them (false).
# Defaults to true.
# See: http://www.rubydoc.info/gems/lotus-controller/#Exceptions_management
#
# handle_exceptions true
##
# HTTP
#
# Routes definitions for this application
# See: http://www.rubydoc.info/gems/lotus-router#Usage
#
routes 'config/routes'
# URI scheme used by the routing system to generate absolute URLs
# Defaults to "http"
#
# scheme 'https'
# URI host used by the routing system to generate absolute URLs
# Defaults to "localhost"
#
# host 'example.org'
# URI port used by the routing system to generate absolute URLs
# Argument: An object coercible to integer, default to 80 if the scheme is http and 443 if it's https
# This SHOULD be configured only in case the application listens to that non standard ports
#
# port 443
# Enable cookies
#
# cookies true
# Enable sessions
# Argument: Symbol the Rack session adapter
# A Hash with options
#
# See: http://www.rubydoc.info/gems/rack/Rack/Session/Cookie
#
# sessions :cookie, secret: ENV['WEB_SESSIONS_SECRET']
# Configure Rack middleware for this application
#
# middleware.use Rack::Protection
# Default format for the requests that don't specify an HTTP_ACCEPT header
# Argument: A symbol representation of a mime type, default to :html
#
# default_format :json
# HTTP Body parsers
# Parse non GET responses body for a specific mime type
# Argument: Symbol, which represent the format of the mime type (only `:json` is supported)
# Object, the parser
#
# body_parsers :json
##
# DATABASE
#
# Configure a database adapter
# Argument: A Hash with the settings
# type: Symbol, :file_system, :memory and :sql
# uri: String, 'file:///db/bookshelf'
# 'memory://localhost/bookshelf'
# 'sqlite:memory:'
# 'sqlite://db/bookshelf.db'
# 'postgres://localhost/bookshelf'
# 'mysql://localhost/bookshelf'
#
adapter type: :sql, uri: ENV['GRANDMA_DATABASE_URL']
# Configure a database mapping
# See: http://www.rubydoc.info/gems/lotus-model#Data_Mapper
#
# mapping 'config/mapping'
##
# TEMPLATES
#
# The layout to be used by all the views
#
layout :application # It will load Web::Views::ApplicationLayout
# The relative path where to find the templates
#
# templates 'templates'
##
# ASSETS
#
# Specify sources for assets
# The directory `public/` is added by default
#
# assets << [
# 'vendor/javascripts'
# ]
# Enabling serving assets
# Defaults to false
#
# serve_assets false
##
# FRAMEWORKS
#
# Configure the code to be yielded each time Web::Action will be included
# This is useful for share common functionalities
#
# See: http://www.rubydoc.info/gems/lotus-controller#Configuration
controller.prepare do
# include MyAuthentication # included in all the actions
# before :authenticate! # run an authentication before callback
end
# Configure the code to be yielded each time Web::View will be included
# This is useful for share common functionalities
#
# See: http://www.rubydoc.info/gems/lotus-view#Configuration
view.prepare do
# include MyRoutingHelpers # included in all the views
end
end
##
# DEVELOPMENT
#
configure :development do
# Don't handle exceptions, render the stack trace
handle_exceptions false
# Serve static assets during development
serve_assets true
end
##
# TEST
#
configure :test do
# Don't handle exceptions, render the stack trace
handle_exceptions false
# Serve static assets during development
serve_assets true
end
##
# PRODUCTION
#
configure :production do
# scheme 'https'
# host 'example.org'
# port 443
end
end
end
# Configure your database mapping here
# See: http://www.rubydoc.info/gems/lotus-model/#Usage
#
# collection :users do
# entity User
# repository UserRepository
#
# attribute :id, Integer
# attribute :name, String
# end
collection :categories do
entity Category
attribute :id, Integer
attribute :name, String
end
# Define ENV variables for development environment
GRANDMA_DATABASE_URL="postgres://localhost/grandma_development?user=postgres"
require 'lotus/model'
Dir["#{ __dir__ }/**/*.rb"].each { |file| require_relative file }
Lotus::Model.configure do
# Database adapter
#
# Available options:
#
# * Memory adapter
# adapter type: :memory, uri: 'memory://localhost/grandma_development'
#
# * SQL adapter
# adapter type: :sql, uri: 'sqlite://db/grandma_development.db'
# adapter type: :sql, uri: 'postgres://localhost/grandma_development'
# adapter type: :sql, uri: 'mysql://localhost/grandma_development'
#
adapter type: :sql, uri: ENV['GRANDMA_DATABASE_URL']
##
# Database mapping
#
mapping do
# collection :users do
# entity User
# repository UserRepository
#
# attribute :id, Integer
# attribute :name, String
# end
end
end.load!
require 'lotus/model'
class Category
include Lotus::Entity
attributes :name
end
require 'lotus/model'
class CategoryRepository
include Lotus::Repository
def self.by_name
query do
order(:name)
end
end
end
[5] pry(main)> Lotus::Model.configuration
=> #<Lotus::Model::Configuration:0x007fbccb604430
@adapter=
#<Lotus::Model::Adapters::FileSystemAdapter:0x007fbccb464f30
@_mutex=#<Mutex:0x007fbccb464800>,
@collections={},
@mapper=#<Lotus::Model::Mapper:0x007fbccb5fadb8 @coercer=Lotus::Model::Mapping::Coercer, @collections={}>,
@mutex=#<Mutex:0x007fbccb464ee0>,
@root=#<Pathname:db/grandma_development>,
@uri="file:///db/grandma_development">,
@adapter_config=
#<Lotus::Model::Config::Adapter:0x007fbccb5fb538
@class_name=#<Lotus::Utils::String:0x007fbccb5faed0 @string="FileSystemAdapter">,
@type=:file_system,
@uri="file:///db/grandma_development">,
@mapper=#<Lotus::Model::Mapper:0x007fbccb5fadb8 @coercer=Lotus::Model::Mapping::Coercer, @collections={}>,
@mapper_config=
#<Lotus::Model::Config::Mapper:0x007fbccb5fade0
@blk=#<Proc:0x007fbccb5fae58@/home/rob/Sites/g/grandma/lib/grandma.rb:22>>>
[6] pry(main)> CategoryRepository.new
=> #<CategoryRepository:0x007fbccb973120>
[7] pry(main)> CategoryRepository
=> CategoryRepository
[8] pry(main)> c = Category.new(name: "Test")
=> #<Category:0x007fbccb8e7d78 @name="Test">
[9] pry(main)> CategoryRepository.create(c)
Lotus::Model::Adapters::NoAdapterError: Cannot invoke `create' without selecting an adapter. Please check your framework configuration.
from /home/rob/Sites/g/grandma/vendor/gems/ruby/2.1.0/bundler/gems/model-af99b67ecb7e/lib/lotus/model/adapters/null_adapter.rb:15:in `method_missing'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment