class

Orma::Record

Inherits Reference < Object

Macros

accessible(access_model, target_resource, refreshed_template, &blk)

Provides the capability to share records via access tokens.

Adds the following to the model:

  • An access_token column that will be populated automatically
  • The access_view macro which defines a template to be shown when opening a link containing the access_token
    • access_view can be called in the block of the accessible call
  • A page providing the access_view template
    • The path will be /<model_name>/access/<access_token>
  • #share_uri, returning the full URI to open the access_view
  • #share_element, returning a wrapper template that will attempt to trigger sharing capabilities on the client device on click
    • Sharing the #share_uri via the Web Share API (mostly on mobile devices)
    • Writing the #share_uri to the device clipboard via the Clipboard API (usually only available in secure contexts)
  • A model action to accept access
    • Its visible template can be defined via the accept_access_view template macro
    • Should be included via #accept_access_action_template in the access_view template
  • The access_model_attributes macro, accepting a NamedTuple parameter
    • The given attributes will be added to the access model instance that is created when a user accepts the access via the model action

NOTE: This macro is about sharing and granting access, not about restricting it. Any Resource showing this model has to be made aware of any restrictions by other means.

Example usage:

class GroupMember < ApplicationRecord
  column session_id : String
end

class Group < ApplicationRecord
  [...]

  model_template :member_list do
    [...]
  end

  accessible GroupMember, GroupResource, member_list do
    access_view do
      template do
        h1 { "Join!" }

        model.accept_access_action_template
      end
    end

    accept_access_view do
      template do
        button { "Join" }
      end
    end

    access_model_attributes session_id: ctx.session.id.to_s
  end
end
Source
boolean_flip_action(name, attr, tpl, &blk)
Source
create_child_action(name, child_class, parent_id_attr, tpl, &blk)

Defines a model action that creates a child record and merges:

  • parent_params
  • submitted form values
  • optional context_attributes

Form blocks in model actions are model-aware by default (Crumble::ModelForm), so option helpers can directly access model.

Example:

create_child_action :create_reimbursement, Reimbursement, group_id, default_view do
  form do
    field amount : Float64, attrs: {required: true, step: ".01"}
    field recipient_membership_id : Int64, type: :select, options: recipient_options

    def recipient_options
      options = [{"", t.form.recipient_membership_id_prompt}] of Tuple(String, String)
      model.group_memberships.each do |membership|
        next if membership.user_id == ctx.session.user_id
        options << {membership.id.value.to_s, membership.display_name}
      end
      options
    end
  end
end
Source
delete_record_action(name, tpl, &blk)

Defines an action to delete the record from the database. Parameters:

  • name - the name of the action
  • tpl - the model template to render in the response
Source
model_action(name, refreshed_model_template, base_class = Orma::ModelAction, &blk)
Source
model_template(method_name, wrapper_attributes = nil, &blk)
Source
reorder_children_action(name, assoc, child_view, tpl, &blk)

Defines an action to make a list of child models sortable via dragging. Parameters:

  • name - the name of the action
  • assoc - method of the parent model returning the children collection
  • child_view - method of the child model returning the template to use in the sortable list
  • tpl - the model template to render in the response
Source

Nested types