Skip to content

Commit

Permalink
close #2202 improve set up google wallet (#2203)
Browse files Browse the repository at this point in the history
  • Loading branch information
nangdypanhar authored Jan 9, 2025
1 parent 7c1e959 commit 4ebd9f9
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 55 deletions.
65 changes: 38 additions & 27 deletions app/controllers/spree/admin/google_wallets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
module Spree
module Admin
class GoogleWalletsController < Spree::Admin::ResourceController
before_action :product
skip_before_action :load_resource
before_action :product, :object
update.before :clear_verify_status

# POST /google_wallets
def create
wallet = model_class.new(product_id: product.id, review_status: permitted_resource_params[:review_status])
if wallet.save
redirect_to edit_admin_product_google_wallet_path(product, wallet.id)
else
render :new
end
end
helper 'spree/admin/google_wallets'

# POST /google_wallets/:id/create_google_wallet_class
def create_google_wallet_class
service = object.class_creator.call
if service[:status] == '200'
redirect_to location_after_save, notice: I18n.t('google_wallet.google_wallet_class_created')
# POST /google_wallets/:id/verify_with_google
def verify_with_google
if object.preferred_response.blank?
create_google_wallet_class
else
redirect_to location_after_save, alert: I18n.t('google_wallet.google_wallet_class_create_fail')
end
end

# PATCH /google_wallets/:id/update_google_wallet_class
def update_google_wallet_class
service = object.class_updater.call
if service[:status] == '200'
redirect_to location_after_save, notice: I18n.t('google_wallet.google_wallet_class_updated')
else
redirect_to location_after_save, alert: I18n.t('google_wallet.google_wallet_class_update_fail')
update_google_wallet_class
end
end

Expand All @@ -50,7 +33,15 @@ def product
end

def object
@object ||= product.google_wallet
@object ||= if new_actions.include?(action)
model_class.new(product_id: product.id)
elsif params[:id]
product.google_wallet
end
end

def clear_verify_status
permitted_resource_params[:preferred_verified_at] = nil
end

def object_name
Expand All @@ -61,6 +52,26 @@ def location_after_save
edit_admin_product_google_wallet_path(product, object.id)
end

def create_google_wallet_class
service = object.class_creator.call
if service[:status] == '200'
object.verify_create!(service[:status])
redirect_to location_after_save, notice: I18n.t('google_wallet.google_wallet_class_created')
else
redirect_to location_after_save, alert: I18n.t('google_wallet.google_wallet_class_create_fail')
end
end

def update_google_wallet_class
service = object.class_updater.call
if service[:status] == '200'
object.verify!
redirect_to location_after_save, notice: I18n.t('google_wallet.google_wallet_class_updated')
else
redirect_to location_after_save, alert: I18n.t('google_wallet.google_wallet_class_update_fail')
end
end

# @overrided
def model_class
SpreeCmCommissioner::EventTicketGoogleWallet
Expand Down
73 changes: 73 additions & 0 deletions app/helpers/spree/admin/google_wallets_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
module Spree
module Admin
module GoogleWalletsHelper
# override
def preference_fields(object, form)
return unless object.respond_to?(:preferences)

fields = object.preferences.keys.map do |key|
next unless object.has_preference?(key)
next if %i[response verified_at].include?(key)

render_preference_field(object, form, key)
end
safe_join(fields)
end

private

def render_preference_field(object, form, key)
case key
when :background_color
render_background_color_field(object, form, key)
when :start_date, :end_date
render_date_field(object, form, key)
when object.preference_type(key).to_sym == :boolean
render_boolean_field(object, form, key)
else
render_default_field(object, form, key)
end
end

def render_background_color_field(object, form, key)
content_tag(:div, class: 'form-group', id: [object.class.to_s.parameterize, 'preference', key].join('-')) do
form.label("preferred_#{key}", Spree.t(key), class: 'mb-2') +
content_tag(:div, class: 'd-flex') do
preference_field_for(form, "preferred_#{key}", type: object.preference_type(key), class: 'form-control mr-2') +
content_tag(:div, '', class: 'color-preview',
style: "background-color: #{object.preferred_background_color};
height: 40px; width: 50px; margin-left: 10px;
border-radius: 5px; border: 1px solid #ccc;"
)
end
end
end

def render_boolean_field(object, form, key)
content_tag(:div, preference_field_for(form, "preferred_#{key}", type: object.preference_type(key)) +
form.label("preferred_#{key}", Spree.t(key), class: 'form-check-label'),
class: 'form-group form-check', id: [object.class.to_s.parameterize, 'preference', key].join('-')
)
end

def render_date_field(object, form, key)
content_tag(:div, class: 'form-group', id: "#{object.class.to_s.parameterize}-preference-#{key}") do
form.label("preferred_#{key}", Spree.t(key), class: 'mb-2') +
content_tag(:div, class: 'input-group datePickerTo', data: {
wrap: 'true', 'alt-input': 'true', 'min-date': object.preferred_start_date, 'enable-time': 'true'
}
) do
form.text_field("preferred_#{key}", value: object.send("preferred_#{key}"), class: 'form-control shadow-none', 'data-input' => '')
end
end
end

def render_default_field(object, form, key)
content_tag(:div, form.label("preferred_#{key}", Spree.t(key)) +
preference_field_for(form, "preferred_#{key}", type: object.preference_type(key)),
class: 'form-group', id: [object.class.to_s.parameterize, 'preference', key].join('-')
)
end
end
end
end
27 changes: 25 additions & 2 deletions app/models/spree_cm_commissioner/event_ticket_google_wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class EventTicketGoogleWallet < GoogleWallet
preference :start_date, :string
preference :end_date, :string
preference :background_color, :string
preference :response, :string
preference :verified_at, :string

validates :preferred_issuer_name, presence: true, on: :update
validates :preferred_event_name, presence: true, on: :update
Expand All @@ -22,11 +24,32 @@ class EventTicketGoogleWallet < GoogleWallet
def set_default_preferences
self.preferred_issuer_name ||= product.vendor.name
self.preferred_event_name ||= product.name
self.preferred_venue_name ||= product.vendor.stock_location.city
self.preferred_venue_address ||= product.vendor.stock_location.address1
self.preferred_venue_name ||= product.venue.place.name
self.preferred_venue_address ||= product.venue.place.vicinity
self.preferred_start_date = event_start_date unless event_start_date.nil?
self.preferred_end_date = event_end_date unless event_end_date.nil?
self.preferred_background_color = '#000000'
self.preferred_response = nil
self.preferred_verified_at = nil
end

def verified?
preferred_verified_at.present? && preferred_response.present?
end

def verify_create!(response)
self.preferred_verified_at = DateTime.current
self.preferred_response = response if response.present?
save!
end

def verify!
self.preferred_verified_at = DateTime.current
save!
end

def verified_at
DateTime.parse(preferred_verified_at).strftime('%B %d, %Y %I:%M %p') if preferred_verified_at.present?
end

def event_start_date
Expand Down
2 changes: 0 additions & 2 deletions app/models/spree_cm_commissioner/google_wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class GoogleWallet < SpreeCmCommissioner::Base
attachable.variant :large, resize_to_limit: [960, 480]
end

validates :hero_image, presence: true, on: :update

def class_creator
raise NotImplementedError, 'class_creator must be implemented in subclasses'
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def logo
end

def hero_image
rails_blob_url(@google_wallet_class.hero_image)
(product_image_url.presence || rails_blob_url(@google_wallet_class.hero_image))
end

def start_date
Expand All @@ -72,6 +72,10 @@ def vendor_logo_url
@google_wallet_class.product.vendor.logo&.original_url
end

def product_image_url
@google_wallet_class.product.images.first&.original_url
end

def build_request_body
{
id: class_id,
Expand Down
9 changes: 4 additions & 5 deletions app/views/spree/admin/google_wallets/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<%= render 'shared/asset_field',
field: :logo,
label: 'Logo',
asset: @object.logo,
asset: @object.logo.blank? ? @product.vendor.logo : @object.logo ,
remove_url: remove_logo_admin_product_google_wallet_url(@product.slug, @object),
form: f,
classes: ['col-md-5 ml-3']
Expand All @@ -41,12 +41,11 @@
<%= render 'shared/asset_field',
field: :hero_image,
label: 'Hero Image',
asset: @object.hero_image,
remove_url: remove_hero_image_admin_product_google_wallet_url( @product.slug, @object),
asset: @object.hero_image.blank? ? @product.images.first : @object.hero_image,
remove_url: remove_hero_image_admin_product_google_wallet_url(@product.slug, @object),
form:f,
classes: ['col-md-5 ml-3']

%>
%>
</div>
<% if @object.respond_to?(:preferences) %>
<div id="gateway-settings-warning" class="info warning"><%= Spree.t(:provider_settings_warning) %></div>
Expand Down
46 changes: 30 additions & 16 deletions app/views/spree/admin/google_wallets/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
<%= render partial: 'spree/admin/shared/product_tabs', locals: { current: :google_wallet } %>

<div class="my-2 mx-1">
<%= button_to Spree.t(:create), create_google_wallet_class_admin_product_google_wallet_path(@product, @object.id), method: :post, class: 'btn bg-success text-white my-1' %>
<%= button_to Spree.t(:update), update_google_wallet_class_admin_product_google_wallet_path(@product, @object), method: :patch, class: 'btn bg-success text-white my-1' %>
<small class="form-text text-muted">
<%= raw I18n.t('google_wallet.note') %>
</small>
</div>

<div data-hook="admin_product_google_wallet_edit_form_header">
<%= render partial: 'spree/admin/shared/error_messages', locals: { target: @object } %>
<div class="card card-body mb-3">
<div data-hook="active" class="form-group">
<strong><%= Spree.t(:verified) %></strong>
<%= active_badge(@object.verified?) %>
</div>

<div data-hook="admin_product_google_wallet_edit_form">
<%= form_with model: @object, url: { action: 'update' } do |f| %>
<%= render partial: 'form', locals: { f: f } %>
<div data-hook="admin_product_google_wallet_form_buttons">
<%= render partial: 'spree/admin/shared/edit_resource_links' %>
<% if @object.verified_at.present? %>
<div data-hook="verified-at" class="form-group">
<p><%= Spree.t(:verified_at) %>: <%= @object.verified_at %></p>
</div>
<% end %>

<%= button_to Spree.t('verify_with_google_wallet'),
verify_with_google_admin_product_google_wallet_path(@product, @object),
method: :post,
class: 'btn btn-outline-primary my-1',
disabled: @object.verified? %>

<% end %>
<small class="form-text text-muted">
<%= raw I18n.t('google_wallet.note') %>
</small>
</div>


<div data-hook="admin_product_google_wallet_edit_form_header">
<%= render partial: 'spree/admin/shared/error_messages', locals: { target: @object } %>
<div data-hook="admin_product_google_wallet_edit_form">
<%= form_with model: @object, url: { action: 'update' } do |f| %>
<%= render partial: 'form', locals: { f: f } %>
<div data-hook="admin_product_google_wallet_form_buttons">
<%= render partial: 'spree/admin/shared/edit_resource_links' %>
</div>
<% end %>
</div>
3 changes: 1 addition & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@

resources :google_wallets do
member do
post :create_google_wallet_class
patch :update_google_wallet_class
post :verify_with_google
delete :remove_logo
delete :remove_hero_image
end
Expand Down

0 comments on commit 4ebd9f9

Please sign in to comment.