studio-engine 0.47.1 → 0.48.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Resolves Studio.profile_sections — the rows that make up the shared /profile
4
+ # page — for a given view context. Pure Ruby (no Rails dependency) so the unit
5
+ # suite exercises the resolution rules without booting the dummy app.
6
+ #
7
+ # THE SHAPE IS DELIBERATELY THE SIDEBAR'S. Studio.sidebar_sections already taught
8
+ # this codebase one way to let a host declare page furniture: a static Array or a
9
+ # callable that receives the view, symbolized on the way out, with sections the
10
+ # viewer may not see dropped before render. Reusing that shape means a host that
11
+ # has declared a sidebar already knows how to declare a profile.
12
+ #
13
+ # What a section looks like:
14
+ #
15
+ # { key: :avatar, title: "Profile photo", partial: "studio/profiles/avatar_section",
16
+ # locals: { ... }, requires: :avatar, admin: false }
17
+ #
18
+ # key — stable identifier. A host removes or replaces a default by key,
19
+ # so reordering the defaults never breaks a host's override.
20
+ # title — the row's heading.
21
+ # partial — the partial to render. Host partials are ordinary app views.
22
+ # locals — optional Hash passed to the partial.
23
+ # requires — optional attribute (or Array of them) the CURRENT USER must
24
+ # respond to for the row to render. See below.
25
+ # admin — admin-only row, same rule as the sidebar's.
26
+ # if — optional callable. The row renders only when it returns truthy.
27
+ # Called with the view when it takes an argument, without when it
28
+ # does not. This is the APP-CAPABILITY gate, distinct from
29
+ # `requires:` which is the MODEL gate — and the two really are
30
+ # different questions. Every consumer's users table carries
31
+ # `provider` and `uid`, so `requires:` alone selected the whole
32
+ # fleet for the Google row; mcritchie-industries has those columns
33
+ # AND `auth_methods = %i[magic_link]` with no omniauth gem at all,
34
+ # so it would have rendered a "Link Google Account" button leading
35
+ # nowhere. Having a column is not the same as offering the feature.
36
+ # modals — the row opens the shared crop/saving modals. The PAGE mounts them
37
+ # once (studio/modals/_scoped_host on a "profileModals" store) when
38
+ # any resolved row asks for them, so a row never has to know whether
39
+ # the host app renders a modal host of its own. Two of the five
40
+ # consumers render none at all, and the two that do ship a FORK of
41
+ # the engine's shared host that would shadow it.
42
+ #
43
+ # ON `requires` — this is the whole reason the registry exists rather than a
44
+ # hardcoded page. The consuming apps do NOT agree on their users table:
45
+ # mcritchie-industries has eight columns and no `first_name`; turf-monster has
46
+ # forty. A shared page that assumed a column would raise NoMethodError on every
47
+ # signed-in request in the app that lacked it — the same failure mode
48
+ # Studio.first_name_outstanding? already guards against with respond_to?.
49
+ #
50
+ # So a row DECLARES what it needs and is dropped when the host cannot serve it.
51
+ # mcritchie-industries gets a working profile page with an avatar row and no name
52
+ # row until it installs the standard-profile-columns migration, at which point
53
+ # the row appears with no code change. Silence, not a 500.
54
+ module Studio
55
+ module ProfileSections
56
+ # The page every consumer gets for free. Iteration one is deliberately two
57
+ # rows: the picture and the name. Everything else the account-standardization
58
+ # program lifts (email change, identities, preferences) arrives as further
59
+ # defaults, and a host that has declared its own list keeps working because
60
+ # it composes against `Studio.default_profile_sections` rather than a literal.
61
+ DEFAULTS = [
62
+ { key: :avatar, title: "Profile photo",
63
+ partial: "studio/profiles/avatar_section", requires: :avatar, modals: true },
64
+ { key: :first_name, title: "Your name",
65
+ partial: "studio/profiles/first_name_section", requires: :first_name },
66
+ # Gated on the app OFFERING Google, not merely on having the columns —
67
+ # the same question app/views/sessions/new.html.erb:79 already asks before
68
+ # drawing this identical button on the login page.
69
+ { key: :google, title: "Google account",
70
+ partial: "studio/profiles/google_section", requires: %i[provider uid],
71
+ if: -> { Studio.auth_method?(:google) } }
72
+ ].freeze
73
+
74
+ module_function
75
+
76
+ # A fresh, mutable copy every call. Hosts compose with `+` and sometimes
77
+ # `reject`, and handing out the frozen literal would let one host's edit
78
+ # leak into the next request's page.
79
+ def defaults
80
+ DEFAULTS.map { |section| section.dup }
81
+ end
82
+
83
+ # Declared may be nil (the host has said nothing — it gets the defaults), an
84
+ # Array, or a callable receiving the view context.
85
+ #
86
+ # `nil` meaning "the defaults" rather than "no sections" is the load-bearing
87
+ # choice here: it is what makes a brand-new app's profile page work with an
88
+ # empty initializer, which is the entire point of standardizing this.
89
+ def resolve(declared, view)
90
+ sections = declared.respond_to?(:call) ? declared.call(view) : declared
91
+ sections = defaults if sections.nil?
92
+
93
+ admin = view.respond_to?(:admin?) && view.admin?
94
+ user = view.respond_to?(:current_user) ? view.current_user : nil
95
+
96
+ Array(sections)
97
+ .map { |section| symbolize(section) }
98
+ .reject { |section| section[:admin] && !admin }
99
+ .select { |section| enabled?(section[:if], view) }
100
+ .select { |section| served_by?(user, section[:requires]) }
101
+ end
102
+
103
+ # The app-capability gate. No `if:` means always enabled. The callable takes
104
+ # the view when it wants one and nothing when it does not, so a host can
105
+ # write either `-> { Studio.feature?(:x) }` or `->(view) { view.admin? }`.
106
+ def enabled?(condition, view)
107
+ return true if condition.nil?
108
+
109
+ # A Symbol/String names a method on the VIEW — Rails' own
110
+ # `before_action ..., if: :method_name` convention, and therefore the most
111
+ # natural thing a host will write here.
112
+ #
113
+ # It is handled explicitly because the alternative FAILS OPEN: a Symbol
114
+ # does not answer `call`, so the earlier `return !!condition` coerced
115
+ # `:some_predicate` to true and rendered the row unconditionally — a gate
116
+ # that silently does nothing, in the same permissive direction as the bug
117
+ # this whole `if:` key was added to fix. A host would have had no signal.
118
+ if condition.is_a?(Symbol) || condition.is_a?(String)
119
+ return false unless view.respond_to?(condition)
120
+
121
+ return !!view.public_send(condition)
122
+ end
123
+
124
+ return !!condition unless condition.respond_to?(:call)
125
+
126
+ !!(condition.arity.zero? ? condition.call : condition.call(view))
127
+ end
128
+
129
+ # Can this host's user model serve the row? No requirement means yes — a row
130
+ # that reads nothing off the user (a static explainer, a link out) is always
131
+ # served. A nil user means we are rendering for nobody, and nothing is served.
132
+ def served_by?(user, requires)
133
+ needed = Array(requires).compact
134
+ return true if needed.empty?
135
+ return false if user.nil?
136
+
137
+ needed.all? { |attribute| user.respond_to?(attribute) }
138
+ end
139
+
140
+ def symbolize(hash)
141
+ out = hash.to_h.each_with_object({}) { |(key, value), acc| acc[key.to_sym] = value }
142
+
143
+ # `key` is an IDENTIFIER, so its VALUE symbolizes too — unlike every other
144
+ # field, whose value is data. A host composing by key writes
145
+ # `reject { |s| s[:key] == :avatar }`, and that silently matches nothing if
146
+ # a section declared with string keys kept `"avatar"` as a String. Removing
147
+ # a standard row is the documented seam; it must not depend on which
148
+ # spelling the host happened to use.
149
+ out[:key] = out[:key].to_sym if out[:key].respond_to?(:to_sym)
150
+ out
151
+ end
152
+ end
153
+ end
@@ -1,3 +1,3 @@
1
1
  module Studio
2
- VERSION = "0.47.1"
2
+ VERSION = "0.48.0"
3
3
  end
data/lib/studio.rb CHANGED
@@ -7,6 +7,9 @@ require "studio/environment_banner"
7
7
  require "studio/theme_resolver"
8
8
  require "studio/ui_primitives"
9
9
  require "studio/sidebar_sections"
10
+ require "studio/profile_sections"
11
+ require "studio/profile_image"
12
+ require "studio/oauth_identity"
10
13
  require "studio/username_generator"
11
14
  require "studio/s3"
12
15
  require "studio/image_cache"
@@ -83,6 +86,49 @@ module Studio
83
86
  # end
84
87
  mattr_accessor :sidebar_sections, default: []
85
88
 
89
+ # ---- The shared profile page (/profile) ----
90
+ # The rows that make up /profile. `nil` — the default — means "the engine's
91
+ # standard page", NOT "no rows": a brand-new app with an empty initializer gets
92
+ # a working profile page, which is the entire point of standardizing it.
93
+ #
94
+ # Declare an Array (or a callable receiving the view) to customize. Compose
95
+ # against Studio.default_profile_sections rather than a literal, so a later
96
+ # engine release that adds a standard row delivers it to you:
97
+ #
98
+ # Studio.configure do |config|
99
+ # config.profile_sections = ->(view) {
100
+ # Studio.default_profile_sections +
101
+ # [ { key: :wallet, title: "Identities", partial: "profiles/wallet" } ]
102
+ # }
103
+ # end
104
+ #
105
+ # Drop a standard row by key instead of restating the list:
106
+ #
107
+ # config.profile_sections = Studio.default_profile_sections.reject { |s| s[:key] == :avatar }
108
+ #
109
+ # Shape and resolution rules: lib/studio/profile_sections.rb. A row may declare
110
+ # `requires:` — an attribute the current user must respond to — and is dropped
111
+ # when this host's model does not have it, so the page tolerates the fact that
112
+ # the consuming apps' users tables genuinely disagree.
113
+ mattr_accessor :profile_sections, default: nil
114
+
115
+ # Whether Studio.routes draws /profile (Studio::ProfilesController).
116
+ #
117
+ # ON BY DEFAULT, and it is worth saying why this one can be when
118
+ # draw_admin_emails_routes and draw_onboarding_routes could not: those two
119
+ # claimed helper names a consumer ALREADY OWNED, so drawing them raised
120
+ # `Invalid route name, already in use` while that app's routes.rb loaded and
121
+ # took down its entire route set. `profile` is claimed by none of the five
122
+ # consumers — mcritchie-studio, mcritchie-industries, turf-monster, moms-app
123
+ # and acquisition-studio were each checked (2026-08-14) — and turf-monster's
124
+ # nearest names are complete_profile_account_path / save_profile_account_path,
125
+ # which do not collide.
126
+ #
127
+ # That is exactly why the shared page is /profile and not /account: turf owns
128
+ # account_path, and a shared /account could never be default-on. An app that
129
+ # wants the page gone still sets this false.
130
+ mattr_accessor :draw_profile_routes, default: true
131
+
86
132
  # How long a freshly minted magic link stays live.
87
133
  mattr_accessor :magic_link_ttl, default: 15.minutes
88
134
 
@@ -179,6 +225,17 @@ module Studio
179
225
  # later session may ask again. That is the whole reason this is not a column.
180
226
  FIRST_NAME_SKIP_SESSION_KEY = :onboarding_skipped_first_name
181
227
 
228
+ # How long a first name may be. ONE constant because users.first_name is
229
+ # written from TWO surfaces — the onboarding step (seconds after signup) and
230
+ # /profile (any time after) — and rendered by a third, the profile form's
231
+ # maxlength. Two independently-correct caps that disagreed would let onboarding
232
+ # accept a name /profile then refused to save, a bug with no obvious owner.
233
+ #
234
+ # Keeping it here rather than on either controller also keeps the VIEW off a
235
+ # controller constant: the form needs the number, and a view reaching into
236
+ # Studio::ProfilesController to get it would couple the two for no reason.
237
+ FIRST_NAME_MAX_LENGTH = 40
238
+
182
239
  # The shared rule for "does this account still owe us a first name?" — the one
183
240
  # piece of onboarding logic every app agrees on. Hosts compose it into their own
184
241
  # flow rather than re-deriving it (turf's OnboardingFlow calls straight through).
@@ -545,6 +602,21 @@ module Studio
545
602
  SidebarSections.resolve(sidebar_sections, view)
546
603
  end
547
604
 
605
+ # The engine's standard /profile rows. Hosts compose against this rather than
606
+ # restating a literal list, so a later release that adds a standard row
607
+ # delivers it to every app that used the seam as intended.
608
+ def self.default_profile_sections
609
+ ProfileSections.defaults
610
+ end
611
+
612
+ # Profile rows resolved for a view context: a callable config is called with
613
+ # the view, keys symbolize, admin-only rows drop for non-admin viewers, and
614
+ # rows this host's user model cannot serve drop entirely. `nil` config resolves
615
+ # to the standard page.
616
+ def self.profile_sections_for(view)
617
+ ProfileSections.resolve(profile_sections, view)
618
+ end
619
+
548
620
  def self.env_truthy?(value)
549
621
  %w[1 true yes on].include?(value.to_s.strip.downcase)
550
622
  end
@@ -602,6 +674,23 @@ module Studio
602
674
  post "auth/solana/verify", to: "solana_sessions#verify", as: :solana_verify
603
675
  end
604
676
 
677
+ # The shared profile page. ON by default — unlike /admin/emails and the
678
+ # onboarding pair, `profile` is claimed by no consumer, so drawing it
679
+ # cannot take an app's route set down. See Studio.draw_profile_routes.
680
+ #
681
+ # Avatar is its own PATCH rather than a field on #update: an attachment
682
+ # param submitted empty PURGES the attachment, so a single form carrying
683
+ # both would delete someone's photo every time they edited their name.
684
+ if Studio.draw_profile_routes
685
+ get "profile", to: "studio/profiles#show", as: :profile
686
+ patch "profile", to: "studio/profiles#update"
687
+ patch "profile/avatar", to: "studio/profiles#avatar", as: :profile_avatar
688
+ # DELETE, because unlinking removes an identity. Linking is not drawn
689
+ # here: it is OmniAuth's own /auth/:provider, which the middleware owns.
690
+ delete "profile/google", to: "studio/profiles#unlink_google",
691
+ as: :profile_unlink_google
692
+ end
693
+
605
694
  resources :error_logs, only: [:index, :show]
606
695
 
607
696
  # Admin
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: studio-engine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.47.1
4
+ version: 0.48.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex McRitchie
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-08-14 00:00:00.000000000 Z
11
+ date: 2026-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -204,6 +204,7 @@ files:
204
204
  - app/controllers/studio/local_reviews_controller.rb
205
205
  - app/controllers/studio/models_controller.rb
206
206
  - app/controllers/studio/onboarding_controller.rb
207
+ - app/controllers/studio/profiles_controller.rb
207
208
  - app/controllers/style_controller.rb
208
209
  - app/controllers/theme_settings_controller.rb
209
210
  - app/helpers/studio/admin_models_table_helper.rb
@@ -219,6 +220,7 @@ files:
219
220
  - app/models/concerns/sluggable.rb
220
221
  - app/models/concerns/studio/board/rankable.rb
221
222
  - app/models/concerns/studio/broadcastable.rb
223
+ - app/models/concerns/studio/user_profile.rb
222
224
  - app/models/current.rb
223
225
  - app/models/error_log.rb
224
226
  - app/models/image_cache.rb
@@ -333,6 +335,10 @@ files:
333
335
  - app/views/studio/models/show.html.erb
334
336
  - app/views/studio/newsletter_mailer/subscribed.html.erb
335
337
  - app/views/studio/newsletter_mailer/subscribed.text.erb
338
+ - app/views/studio/profiles/_avatar_section.html.erb
339
+ - app/views/studio/profiles/_first_name_section.html.erb
340
+ - app/views/studio/profiles/_google_section.html.erb
341
+ - app/views/studio/profiles/show.html.erb
336
342
  - app/views/style/_modal_specimen.html.erb
337
343
  - app/views/style/_modals.html.erb
338
344
  - app/views/style/_specimen.html.erb
@@ -373,6 +379,9 @@ files:
373
379
  - lib/studio/link_token.rb
374
380
  - lib/studio/log_rotation.rb
375
381
  - lib/studio/mail_transport.rb
382
+ - lib/studio/oauth_identity.rb
383
+ - lib/studio/profile_image.rb
384
+ - lib/studio/profile_sections.rb
376
385
  - lib/studio/redis.rb
377
386
  - lib/studio/s3.rb
378
387
  - lib/studio/sidebar_sections.rb