Skip to content

Instantly share code, notes, and snippets.

@vincenting
Created November 15, 2015 02:31
Show Gist options
  • Save vincenting/712b89d44e25c4c26f1c to your computer and use it in GitHub Desktop.
Save vincenting/712b89d44e25c4c26f1c to your computer and use it in GitHub Desktop.
A designing enganced model for creating restful webservice.
class Salesman < Sequel::Model(:crm_resources_salesmen)
include Restfy::Model
allow_method :update, :get, :query, :create
exclude_fields_for [:get, :query], :tenantId
exclude_fields_for_update :tenantId, :groupId
include_fields_for_create :id
one_to_many :logs
one_to_many :tasks
one_to_many :houses
one_to_many :contacts
use_timestamp :createdTimestamp, :updatedTimestamp
dynamic_fields :details do
field :sex, type: 'string', required: true, default: 'unknown',
enums: %w(male female unknown)
validate_for_sex do |sex|
end
end
def init(req)
self[:tenantId] = req.current_tenant_id
end
validate_for_name do |name|
raise Error.new if !name || name.empty?
end
authenticate_for(:update, :create) do |req|
where(id: req.current_salesman_id)
end
authenticate_for(:query, :get) do |req|
where(tenantId: req.current_tenant_id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment