Ohm::Scope
Ohm::Scope wraps Ohm::Model to let you work with user
input in a way that is both safe and familiar to Ohm users.
Installation
$ gem install ohm-scope
Usage
Ohm::Scope implements all of Ohm::Model's public class methods while maintaining
the same contract as using Ohm::Model directly. If you're comfortable with Ohm::Model,
you should feel at home using Ohm::Scope. The one method new to Ohm::Scope
is the #build
method. It is a factory method that delegates
to the scope's model class ::new
method by default, but also lets us
pass an argument for a different class if we want to construct something other
than a model.
Example
require 'syro'
require 'ohm'
require 'ohm/scope'
class User < Ohm::Model
collection :posts, :Post
end
class Post < Ohm::Model
attribute :title
unique :title
reference :user, :User
end
class Deck < Syro::Deck
def scope(model, user)
Ohm::Scope.new(model, { user_id: user.id })
end
def curent_user
User.with(:auth_token, req.session[:auth_token])
end
end
App = Syro.new(Deck) do
on 'posts' do
@posts = scope(Post, current_user)
post do
res.json @posts.create(params)
end
on :id do
@post = @posts[inbox[:id]]
patch do
...
end
delete do
...
end
end
end
end
ruby```