Module: SinatraResource::Builder::ActionDefinitions

Defined in:
lib/builder/action_definitions.rb

Instance Method Summary collapse

Instance Method Details

#document_count_for_get_many(model, resource_config, parent_document, child_assoc) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/builder/action_definitions.rb', line 20

def document_count_for_get_many(model, resource_config, parent_document, child_assoc)
  if resource_config[:parent]
    count_nested_documents(parent_document, child_assoc, model)
  else
    count_documents(model)
  end
end

#document_for_delete(role, model, resource_config, leaf, id, parent_document, child_assoc) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/builder/action_definitions.rb', line 78

def document_for_delete(role, model, resource_config, leaf, id, parent_document, child_assoc)
  check_permission(:delete, role, resource_config)
  if resource_config[:parent]
    check_related?(parent_document, child_assoc, id)
  end
  check_params(:delete, role, resource_config, leaf)
  document = if resource_config[:parent]
    find_nested_document!(parent_document, child_assoc, model, id)
  else
    find_document!(model, id)
  end
  do_callback(:before_destroy, resource_config, document, parent_document)
  document = if resource_config[:parent]
    delete_nested_document!(parent_document, child_assoc, model, id)
  else
    delete_document!(model, id)
  end
  do_callback(:after_destroy, resource_config, document, parent_document)
  document
end

#document_for_get_one(role, model, resource_config, leaf, id, parent_document, child_assoc) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/builder/action_definitions.rb', line 7

def document_for_get_one(role, model, resource_config, leaf, id, parent_document, child_assoc)
  check_permission(:read, role, resource_config)
  if resource_config[:parent]
    check_related?(parent_document, child_assoc, id)
  end
  check_params(:read, role, resource_config, leaf)
  if resource_config[:parent]
    find_nested_document!(parent_document, child_assoc, model, id)
  else
    find_document!(model, id)
  end
end

#document_for_post(role, model, resource_config, leaf, parent_document, child_assoc) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/builder/action_definitions.rb', line 41

def document_for_post(role, model, resource_config, leaf, parent_document, child_assoc)
  check_permission(:create, role, resource_config)
  check_params(:create, role, resource_config, leaf)
  do_callback(:before_create, resource_config, nil, parent_document)
  document = if resource_config[:parent]
    create_nested_document!(parent_document, child_assoc, model)
  else
    create_document!(model)
  end
  if resource_config[:parent]
    make_related(parent_document, document, resource_config)
  end
  do_callback(:after_create, resource_config, document, parent_document)
  document
end

#document_for_put(role, model, resource_config, leaf, id, parent_document, child_assoc) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/builder/action_definitions.rb', line 57

def document_for_put(role, model, resource_config, leaf, id, parent_document, child_assoc)
  check_permission(:update, role, resource_config)
  if resource_config[:parent]
    check_related?(parent_document, child_assoc, id)
  end
  check_params(:update, role, resource_config, leaf)
  document = if resource_config[:parent]
    find_nested_document!(parent_document, child_assoc, model, id)
  else
    find_document!(model, id)
  end
  do_callback(:before_update, resource_config, document, parent_document)
  document = if resource_config[:parent]
    update_nested_document!(parent_document, child_assoc, model, id)
  else
    update_document!(model, id)
  end
  do_callback(:after_update, resource_config, document, parent_document)
  document
end

#documents_for_get_many(role, model, resource_config, page, items_per_page, leaf, parent_document, child_assoc) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/builder/action_definitions.rb', line 28

def documents_for_get_many(role, model, resource_config, page, items_per_page, leaf, parent_document, child_assoc)
  check_permission(:list, role, resource_config)
  check_params(:list, role, resource_config, leaf)
  documents = if resource_config[:parent]
    find_nested_documents!(parent_document, child_assoc, model, page, items_per_page)
  else
    find_documents!(model, page, items_per_page)
  end
  documents.select do |doc|
    authorized?(:read, lookup_role(doc), resource_config)
  end
end