From 7afe62522f14e1db700f7e761b5dc746f0e94213 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Wed, 4 Jul 2018 14:05:44 +0200 Subject: [PATCH] refactor User.as_json to use jbuilder template --- app/models/user.rb | 73 ++------------------- app/views/api/members/_member.json.jbuilder | 59 +++++++++++++++++ app/views/api/members/show.json.jbuilder | 61 +---------------- app/views/application/index.html.erb | 2 +- 4 files changed, 70 insertions(+), 125 deletions(-) create mode 100644 app/views/api/members/_member.json.jbuilder diff --git a/app/models/user.rb b/app/models/user.rb index 6b630072f..d1a70bc50 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -74,72 +74,13 @@ class User < ActiveRecord::Base scope :without_subscription, -> { includes(:subscriptions).where(subscriptions: { user_id: nil }) } scope :with_subscription, -> { joins(:subscriptions) } - def to_builder - Jbuilder.new do |json| - json.id id - json.username username - json.email email - json.role roles.first.name - json.group_id group_id - json.name profile.full_name - json.need_completion need_completion? - json.profile do - json.user_avatar do - json.id profile.user_avatar.id - json.attachment_url profile.user_avatar.attachment_url - end if profile.user_avatar - json.first_name profile.first_name - json.last_name profile.last_name - json.gender profile.gender.to_s - json.birthday profile.birthday.iso8601 if profile.birthday - json.interest profile.interest - json.software_mastered profile.software_mastered - json.address profile.address.address if profile.address - json.phone profile.phone - end - json.subscribed_plan do - json.id subscribed_plan.id - json.name subscribed_plan.name - json.base_name subscribed_plan.base_name - json.amount (subscribed_plan.amount / 100.0) - json.interval subscribed_plan.interval - json.interval_count subscribed_plan.interval_count - json.training_credit_nb subscribed_plan.training_credit_nb - json.training_credits subscribed_plan.training_credits do |tc| - json.training_id tc.creditable_id - end - json.machine_credits subscribed_plan.machine_credits do |mc| - json.machine_id mc.creditable_id - json.hours mc.hours - end - end if subscribed_plan - json.subscription do - json.id subscription.id - json.expired_at subscription.expired_at.iso8601 - json.canceled_at subscription.canceled_at.iso8601 if subscription.canceled_at - json.stripe subscription.stp_subscription_id.present? - json.plan do - json.id subscription.plan.id - json.base_name subscription.plan.base_name - json.name subscription.plan.name - json.interval subscription.plan.interval - json.interval_count subscription.plan.interval_count - json.amount subscription.plan.amount ? (subscription.plan.amount / 100.0) : 0 - end - end if subscription - json.training_credits training_credits do |tc| - json.training_id tc.creditable_id - end - json.machine_credits machine_credits do |mc| - json.machine_id mc.creditable_id - json.hours_used mc.users_credits.find_by(user_id: id).hours_used - end - json.last_sign_in_at last_sign_in_at.iso8601 if last_sign_in_at - end - end - - def to_json(options) - to_builder.target! + def as_json + ApplicationController.new.view_context.render( + partial: 'api/members/member', + locals: { :member => self }, + formats: [:json], + handlers: [:jbuilder] + ) end def self.admins diff --git a/app/views/api/members/_member.json.jbuilder b/app/views/api/members/_member.json.jbuilder new file mode 100644 index 000000000..6926e6a13 --- /dev/null +++ b/app/views/api/members/_member.json.jbuilder @@ -0,0 +1,59 @@ +json.extract! member, :id, :username, :email, :group_id +json.role member.roles.first.name +json.name member.profile.full_name +json.need_completion member.need_completion? +json.profile do + json.id member.profile.id + json.user_avatar do + json.id member.profile.user_avatar.id + json.attachment_url member.profile.user_avatar.attachment_url + end if member.profile.user_avatar + json.first_name member.profile.first_name + json.last_name member.profile.last_name + json.gender member.profile.gender.to_s + json.birthday member.profile.birthday.to_date.iso8601 if member.profile.birthday + json.interest member.profile.interest + json.software_mastered member.profile.software_mastered + json.address do + json.id member.profile.address.id + json.address member.profile.address.address + end if member.profile.address + json.phone member.profile.phone + json.website member.profile.website + json.job member.profile.job + json.extract! member.profile, :facebook, :twitter, :google_plus, :viadeo, :linkedin, :instagram, :youtube, :vimeo, :dailymotion, :github, :echosciences, :pinterest, :lastfm, :flickr + json.organization do + json.id member.profile.organization.id + json.name member.profile.organization.name + json.address do + json.id member.profile.organization.address.id + json.address member.profile.organization.address.address + end if member.profile.organization.address + end if member.profile.organization + +end +json.subscribed_plan do + json.partial! 'api/shared/plan', plan: member.subscribed_plan +end if member.subscribed_plan +json.subscription do + json.id member.subscription.id + json.expired_at member.subscription.expired_at.iso8601 + json.canceled_at member.subscription.canceled_at.iso8601 if member.subscription.canceled_at + json.stripe member.subscription.stp_subscription_id.present? + json.plan do + json.id member.subscription.plan.id + json.base_name member.subscription.plan.base_name + json.name member.subscription.plan.name + json.interval member.subscription.plan.interval + json.interval_count member.subscription.plan.interval_count + json.amount member.subscription.plan.amount ? (member.subscription.plan.amount / 100.0) : 0 + end +end if member.subscription +json.training_credits member.training_credits do |tc| + json.training_id tc.creditable_id +end +json.machine_credits member.machine_credits do |mc| + json.machine_id mc.creditable_id + json.hours_used mc.users_credits.find_by(user_id: member.id).hours_used +end +json.last_sign_in_at member.last_sign_in_at.iso8601 if member.last_sign_in_at diff --git a/app/views/api/members/show.json.jbuilder b/app/views/api/members/show.json.jbuilder index 9074edbe3..b577818fb 100644 --- a/app/views/api/members/show.json.jbuilder +++ b/app/views/api/members/show.json.jbuilder @@ -1,56 +1,8 @@ requested_current = (current_user and current_user.id == @member.id) -json.extract! @member, :id, :uid, :username, :email, :group_id, :slug, :invoicing_disabled, :is_allow_contact, :is_allow_newsletter -json.role @member.roles.first.name -json.name @member.profile.full_name -json.need_completion @member.need_completion? -json.profile do - json.id @member.profile.id - json.user_avatar do - json.id @member.profile.user_avatar.id - json.attachment_url @member.profile.user_avatar.attachment_url - end if @member.profile.user_avatar - json.first_name @member.profile.first_name - json.last_name @member.profile.last_name - json.gender @member.profile.gender.to_s - json.birthday @member.profile.birthday.to_date.iso8601 if @member.profile.birthday - json.interest @member.profile.interest - json.software_mastered @member.profile.software_mastered - json.address do - json.id @member.profile.address.id - json.address @member.profile.address.address - end if @member.profile.address - json.phone @member.profile.phone - json.website @member.profile.website - json.job @member.profile.job - json.extract! @member.profile, :facebook, :twitter, :google_plus, :viadeo, :linkedin, :instagram, :youtube, :vimeo, :dailymotion, :github, :echosciences, :pinterest, :lastfm, :flickr - json.organization do - json.id @member.profile.organization.id - json.name @member.profile.organization.name - json.address do - json.id @member.profile.organization.address.id - json.address @member.profile.organization.address.address - end if @member.profile.organization.address - end if @member.profile.organization +json.partial! 'api/members/member', member: @member +json.extract! @member, :uid, :slug, :invoicing_disabled, :is_allow_contact, :is_allow_newsletter -end -json.subscribed_plan do - json.partial! 'api/shared/plan', plan: @member.subscribed_plan -end if @member.subscribed_plan -json.subscription do - json.id @member.subscription.id - json.expired_at @member.subscription.expired_at.iso8601 - json.canceled_at @member.subscription.canceled_at.iso8601 if @member.subscription.canceled_at - json.stripe @member.subscription.stp_subscription_id.present? - json.plan do - json.id @member.subscription.plan.id - json.base_name @member.subscription.plan.base_name - json.name @member.subscription.plan.name - json.interval @member.subscription.plan.interval - json.interval_count @member.subscription.plan.interval_count - json.amount @member.subscription.plan.amount ? (@member.subscription.plan.amount / 100.0) : 0 - end -end if @member.subscription json.training_ids @member.training_ids json.trainings @member.trainings do |t| json.id t.id @@ -64,14 +16,7 @@ json.training_reservations @member.reservations.where(reservable_type: 'Training json.is_valid @member.training_ids.include?(r.reservable_id) json.canceled_at r.slots.first.canceled_at end -json.training_credits @member.training_credits do |tc| - json.training_id tc.creditable_id -end -json.machine_credits @member.machine_credits do |mc| - json.machine_id mc.creditable_id - json.hours_used mc.users_credits.find_by(user_id: @member.id).hours_used -end -json.last_sign_in_at @member.last_sign_in_at.iso8601 if @member.last_sign_in_at + json.all_projects @member.all_projects do |project| if requested_current || project.state == 'published' json.extract! project, :id, :name, :description, :author_id, :licence_id, :slug, :state diff --git a/app/views/application/index.html.erb b/app/views/application/index.html.erb index eb7111e73..470ee2976 100644 --- a/app/views/application/index.html.erb +++ b/app/views/application/index.html.erb @@ -77,7 +77,7 @@ - +
<%= flash_messages %>