Class: AgentCode::Blueprint::Generators::TestGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/agentcode/blueprint/generators/test_generator.rb

Overview

Generates RSpec request spec files with per-role contexts and individual action tests.

Constant Summary collapse

ALL_ACTIONS =
%w[index show store update destroy trashed restore forceDelete].freeze
ACTION_LABELS =
{
  "index" => "list",
  "show" => "show",
  "store" => "create",
  "update" => "update",
  "destroy" => "delete",
  "trashed" => "view trashed",
  "restore" => "restore",
  "forceDelete" => "force delete"
}.freeze

Instance Method Summary collapse

Instance Method Details

#actions_to_permissions(actions, slug) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/agentcode/blueprint/generators/test_generator.rb', line 37

def actions_to_permissions(actions, slug)
  if (ALL_ACTIONS - actions).empty?
    "['#{slug}.*']"
  else
    items = actions.map { |a| "'#{slug}.#{a}'" }
    "[#{items.join(', ')}]"
  end
end

#generate(blueprint, is_multi_tenant, org_identifier = "slug") ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/agentcode/blueprint/generators/test_generator.rb', line 21

def generate(blueprint, is_multi_tenant, org_identifier = "slug")
  model = blueprint[:model]
  slug = blueprint[:slug]
  permissions = blueprint[:permissions]
  columns = blueprint[:columns]
  factory_name = model_to_factory(model)

  role_contexts = build_role_contexts(slug, factory_name, permissions, columns, is_multi_tenant, org_identifier)

  if is_multi_tenant
    wrap_multi_tenant(model, slug, role_contexts, org_identifier)
  else
    wrap_non_tenant(model, slug, role_contexts)
  end
end