Class: Studio::ModelPage

Inherits:
Object
  • Object
show all
Defined in:
app/models/studio/model_page.rb

Overview

The model-page protocol (v1).

A reusable, per-record inspector. Given a whitelisted model key and a record identifier, it produces the two things a v1 model page renders — the record as pretty-printed JSON and a copy/paste rails-console command that reloads it — plus the lookup a "random sample" jump needs. The controller and view stay thin; enabling a model is a one-line registration.

The engine ships an EMPTY registry: no model is reachable until a host app opts in. Each app registers its own models in an initializer, e.g.

Studio::ModelPage.register("release", Release, lookup: :slug)

This mirrors the admin-models pattern — the host hands the engine live class references; the engine never constantizes host models by name. An unknown key raises UnknownModel, which the controller renders as 404.

Defined Under Namespace

Classes: Entry, UnknownModel

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_key, identifier = nil) ⇒ ModelPage

identifier is the record's lookup-key value from the URL (nil for /random, which addresses the model as a whole rather than one record).



63
64
65
66
67
# File 'app/models/studio/model_page.rb', line 63

def initialize(model_key, identifier = nil)
  @entry = self.class.entry(model_key)
  @model_key = model_key.to_s
  @identifier = identifier
end

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



59
60
61
# File 'app/models/studio/model_page.rb', line 59

def identifier
  @identifier
end

#model_keyObject (readonly)

Returns the value of attribute model_key.



59
60
61
# File 'app/models/studio/model_page.rb', line 59

def model_key
  @model_key
end

Class Method Details

.entry(key) ⇒ Object



48
49
50
# File 'app/models/studio/model_page.rb', line 48

def entry(key)
  registry.fetch(key.to_s) { raise UnknownModel, "unknown model: #{key.inspect}" }
end

.keysObject



39
40
41
# File 'app/models/studio/model_page.rb', line 39

def keys
  registry.keys
end

.register(key, model, lookup: :slug) ⇒ Object

Enable a model. model is the live AR class; lookup is the column its page URL is keyed on (usually its to_param backing). Returns the key.



30
31
32
33
# File 'app/models/studio/model_page.rb', line 30

def register(key, model, lookup: :slug)
  registry[key.to_s] = Entry.new(model: model, lookup: lookup.to_sym)
  key.to_s
end

.registered?(key) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'app/models/studio/model_page.rb', line 35

def registered?(key)
  registry.key?(key.to_s)
end

.reset!Object

Clears the registry — for tests and boot-time re-registration.



44
45
46
# File 'app/models/studio/model_page.rb', line 44

def reset!
  registry.clear
end

Instance Method Details

#console_commandObject

The copy/paste rails-console command that reloads this record, e.g. Release.find_by(slug: "rel-20260707-a1b2c3")



91
92
93
# File 'app/models/studio/model_page.rb', line 91

def console_command
  %(#{model.name}.find_by(#{lookup_key}: #{identifier.inspect}))
end

#identifier_for(record) ⇒ Object

The lookup-key value to route to for a given record (used by /random).



101
102
103
# File 'app/models/studio/model_page.rb', line 101

def identifier_for(record)
  record.public_send(lookup_key)
end

#jsonObject

The record serialized as pretty JSON — the page's primary payload.



96
97
98
# File 'app/models/studio/model_page.rb', line 96

def json
  ::JSON.pretty_generate(record.as_json)
end

#lookup_keyObject



73
74
75
# File 'app/models/studio/model_page.rb', line 73

def lookup_key
  @entry.lookup
end

#modelObject



69
70
71
# File 'app/models/studio/model_page.rb', line 69

def model
  @entry.model
end

#random_recordObject

A random record of this model, for the "random sample" jump (nil if empty).



85
86
87
# File 'app/models/studio/model_page.rb', line 85

def random_record
  model.order(::Arel.sql("RANDOM()")).first
end

#recordObject

The record this page addresses (nil when the identifier matches nothing).



78
79
80
81
82
# File 'app/models/studio/model_page.rb', line 78

def record
  return @record if defined?(@record)

  @record = model.find_by(lookup_key => identifier)
end