Move profile and user json generation to Jbuilder partials
This commit is contained in:
parent
bf64d85852
commit
94f3c13a70
5 changed files with 16 additions and 17 deletions
|
@ -102,13 +102,6 @@ class Profile < ApplicationRecord
|
|||
Rails.application.routes.url_helpers.profile_path(self)
|
||||
end
|
||||
|
||||
def as_json(options = {})
|
||||
h = super(options)
|
||||
h[:pic_tiny] = get_profile_picture :tiny
|
||||
h[:url] = url
|
||||
h
|
||||
end
|
||||
|
||||
# Cache setters
|
||||
|
||||
def set_caches!
|
||||
|
|
|
@ -84,14 +84,6 @@ class User < ApplicationRecord
|
|||
false
|
||||
end
|
||||
|
||||
def as_json(options = {})
|
||||
h = super(options)
|
||||
h[:unconfirmed_email] = unconfirmed_email
|
||||
h[:confirmed] = confirmed?
|
||||
h[:profile] = profile.as_json
|
||||
h
|
||||
end
|
||||
|
||||
# This is useful for manually generating a Devise user confirmation token so that we can get the confirmation URL with the correct token from anywhere
|
||||
def make_confirmation_token!
|
||||
raw, db = Devise.token_generator.generate(User, :confirmation_token)
|
||||
|
|
6
app/views/app_data/_profile.jbuilder
Normal file
6
app/views/app_data/_profile.jbuilder
Normal file
|
@ -0,0 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
||||
json.extract! profile, :id, :name, :country, :picture
|
||||
json.url profile_path(profile)
|
||||
json.pic_tiny profile.get_profile_pic(:tiny)
|
8
app/views/app_data/_user.jbuilder
Normal file
8
app/views/app_data/_user.jbuilder
Normal file
|
@ -0,0 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
||||
json.extract! user, :id, :created_at, :updated_at
|
||||
json.unconfirmed_email user.unconfirmed_email
|
||||
json.confirmed user.confirmed?
|
||||
|
||||
json.partial! render 'app_data/profile', profile: user.profile
|
|
@ -7,9 +7,9 @@ var app = {
|
|||
, current_admin: <%= !!(current_user && current_role?(:super_admin)) %>
|
||||
, nonprofit: <%= @nonprofit ? raw(@nonprofit.to_json(root:false)) : 'undefined' %>
|
||||
, nonprofit_id : <%= @nonprofit ? @nonprofit.id : 'undefined' %>
|
||||
, user: <%= current_user ? raw(current_user.to_json(root:false)) : 'undefined' %>
|
||||
, user: <%= current_user ? raw("render('app_data/user', user: current_user)") : 'undefined' %>
|
||||
, user_id: <%= current_user ? current_user.id : 'undefined' %>
|
||||
, profile: <%= current_user ? raw(current_user.profile.to_json(root:false)) : 'undefined' %>
|
||||
, profile: <%= current_user ? raw("render('app_data/profile', profile: current_user.profile)") : 'undefined' %>
|
||||
, profile_id: <%= current_user ? current_user.profile.id : 'undefined' %>
|
||||
, asset_path: "<%= Rails.application.config.assets.prefix %>"
|
||||
, host_with_port: "//<%= request.host_with_port %>"
|
||||
|
|
Loading…
Reference in a new issue