Class: BugReportsClient::FormSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/bug_reports_client/form_schema.rb

Overview

Loads and validates the YAML form definition that drives the report form.

Resolution order for the schema file:

1. config.form_schema_path (explicit override)
2. <host app>/config/bug_report_form.yml
3. the engine's config/form_schema.yml default

Schema shape - top-level keys are report types (bug/feature), each holding an ordered list of fields:

bug:
- field: steps_to_reproduce
  type: textarea          # text | textarea | select | checkbox
  required: true
  section: reproduction   # optional - starts a new card in the form
  rows: 4                 # textareas only
  options: [Chrome, Safari]  # selects only
  label: "How can we see this?"   # optional - falls back to i18n
  placeholder: "..."              # optional - falls back to i18n
  help: "..."                     # optional - falls back to i18n

Answers are stored in BugReport#responses keyed by the field name. A feature field named priority doubles as the report's importance rating shown in list views (mirroring severity for bugs).

Defined Under Namespace

Classes: Field

Constant Summary collapse

REPORT_TYPES =
%w[bug feature].freeze
FIELD_TYPES =
%w[text textarea select checkbox].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FormSchema

Returns a new instance of FormSchema.



94
95
96
97
# File 'lib/bug_reports_client/form_schema.rb', line 94

def initialize(path)
  @path = path
  @fields_by_type = parse(path)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



92
93
94
# File 'lib/bug_reports_client/form_schema.rb', line 92

def path
  @path
end

Class Method Details

.currentObject

The active schema. Reloaded on every call in development so schema edits show up without a restart; memoised elsewhere.



61
62
63
64
65
66
67
# File 'lib/bug_reports_client/form_schema.rb', line 61

def current
  if Rails.env.development?
    load_schema
  else
    @current ||= load_schema
  end
end

.default_pathObject



73
74
75
# File 'lib/bug_reports_client/form_schema.rb', line 73

def default_path
  Engine.root.join("config", "form_schema.yml")
end

.reset!Object



69
70
71
# File 'lib/bug_reports_client/form_schema.rb', line 69

def reset!
  @current = nil
end

Instance Method Details

#field_keysObject

Every field key across all report types - used for strong parameters so only schema-declared answers are ever written to responses.



115
116
117
# File 'lib/bug_reports_client/form_schema.rb', line 115

def field_keys
  @fields_by_type.values.flatten.map(&:key).uniq
end

#field_keys_for(report_type) ⇒ Object

Field keys for one report type, falling back to all keys when the type is unknown/blank. Used to scope strong parameters to the selected type so a bug submission can't smuggle in feature answers (and vice versa).



122
123
124
125
# File 'lib/bug_reports_client/form_schema.rb', line 122

def field_keys_for(report_type)
  fields = fields_for(report_type)
  fields.any? ? fields.map(&:key).uniq : field_keys
end

#fields_for(report_type) ⇒ Object



105
106
107
# File 'lib/bug_reports_client/form_schema.rb', line 105

def fields_for(report_type)
  @fields_by_type.fetch(report_type.to_s, [])
end

#report_typesObject

Report types the schema defines, in file order. The form only shows the bug/feature toggle when more than one type is present.



101
102
103
# File 'lib/bug_reports_client/form_schema.rb', line 101

def report_types
  @fields_by_type.keys
end

#required_fields(report_type) ⇒ Object



109
110
111
# File 'lib/bug_reports_client/form_schema.rb', line 109

def required_fields(report_type)
  fields_for(report_type).select(&:required?)
end

#sections_for(report_type) ⇒ Object

Fields grouped into ordered sections for rendering. Fields before any section marker fall into a nil section rendered without a heading.



129
130
131
132
133
# File 'lib/bug_reports_client/form_schema.rb', line 129

def sections_for(report_type)
  fields_for(report_type).chunk_while { |a, b| a.section == b.section }.map do |group|
    [ group.first.section, group ]
  end
end