Skip to content

Commit

Permalink
Stripe: Sprout Initial Stripe Utility
Browse files Browse the repository at this point in the history
#831
#1002

This is mostly because I want to be able to move forward with connecting
Vendor and Distributor accounts in the Marketplace, and to get started
fiddling with the Stripe Connect bits.

This also hopefully helps set the stage to getting rid of `Utility`
classes, and instead have those inherit fro `UtilityHookup` (kind of
like how new `Furniture` inherits from `FurniturePlacement`

To be honest, I should probably either do a rename pass to more clearly
indicate the old-way vs now-and-forward way of defining furniture, or do
a final cleanup pass on both of those before starting this, but this was
the nearest-mess-to-muck-with ( not clean... just.. uhhh... muck with)

Anyway!
  • Loading branch information
zspencer committed Dec 19, 2022
1 parent 1b9ebf7 commit c019ab1
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 17 deletions.
10 changes: 7 additions & 3 deletions app/controllers/utility_hookups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ def index
utility_hookup
end

def new
utility_hookup
end

def edit
utility_hookup
end
Expand All @@ -18,7 +22,7 @@ def create
end

def update
if utility_hookup.update(utility_hookup_params)
if utility_hookup.polymorph.update(utility_hookup_params)
redirect_to edit_space_path(space)
else
render :edit
Expand All @@ -34,12 +38,12 @@ def update

@utility_hookup = policy_scope(space.utility_hookups).find_by(id: params[:id]) if params[:id]
@utility_hookup ||= policy_scope(space.utility_hookups).new(utility_hookup_params)
@utility_hookup.tap { authorize(@utility_hookup) }
authorize(@utility_hookup.polymorph)
end

def utility_hookup_params
return {} unless params[:utility_hookup]
policy(UtilityHookup).permit(params.require(:utility_hookup))
policy(Utilities.fetch(params.dig(:utility_hookup, :utility_slug))).permit(params.require(:utility_hookup))
end

helper_method def space
Expand Down
21 changes: 19 additions & 2 deletions app/models/utility_hookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class UtilityHookup < ApplicationRecord
# has multiple {UtilityHookup}s.
# @return [String]
attribute :name, :string
validates :name, presence: :true, uniqueness: { scope: :space_id }

def name
attributes[:name] ||= utility_slug.to_s.humanize
Expand All @@ -24,8 +25,7 @@ def name
attribute :status, :string, default: "unavailable"
validates :status, presence: true, inclusion: {in: %w[ready unavailable]}

attribute :old_configuration, :json, default: -> { {} }
validates_associated :utility
# validates_associated :utility
has_encrypted :configuration, type: :json

after_initialize do
Expand All @@ -46,4 +46,21 @@ def utility_attributes=(attributes)
self.configuration ||= {}
utility.attributes = attributes
end

def display_name
model_name.human.titleize
end

def form_template
"#{self.class.name.demodulize.underscore}/form"
end

def self.from_utility_hookup(utility_hookup)
utility_hookup.becomes(self)
end

def polymorph
x = Utilities::REGISTRY.fetch(utility_slug&.to_sym, NullUtility)
x.ancestors.include?(UtilityHookup) ? becomes(x) : self
end
end
4 changes: 4 additions & 0 deletions app/policies/application_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ def permitted_attributes(_params)
def policy(object)
Pundit.policy(person, object)
end

def policy!(object)
Pundit.policy!(person, object)
end
end
4 changes: 2 additions & 2 deletions app/policies/utility_hookup_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def destroy?

def permitted_attributes(params)
utility_permitted_attributes =
policy(Utilities.new_from_slug(params[:utility_slug]))
.permitted_attributes(params[:utility_attributes])
policy!(Utilities.new_from_slug(params[:utility_slug]&.to_sym))
&.permitted_attributes(params[:utility_attributes])

[:name, :utility_slug, utility_attributes: utility_permitted_attributes]
end
Expand Down
13 changes: 13 additions & 0 deletions app/utilities/stripe_utility.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class StripeUtility < UtilityHookup
def api_token=api_token
configuration[:api_token] = api_token
end

def api_token
configuration[:api_token]
end

def self.policy_class
Policy
end
end
1 change: 1 addition & 0 deletions app/utilities/stripe_utility/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render "password_field", form: form, attribute: :api_token %>
7 changes: 7 additions & 0 deletions app/utilities/stripe_utility/policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class StripeUtility < UtilityHookup
class Policy < UtilityHookupPolicy
def permitted_attributes(_params)
[:name, :api_token]
end
end
end
12 changes: 9 additions & 3 deletions app/utilities/utilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@
module Utilities
REGISTRY = {
plaid: Plaid::PlaidUtility,
jitsi: Jitsi::JitsiUtility
jitsi: Jitsi::JitsiUtility,
stripe: StripeUtility
}.freeze

# @param utility_hookup [UtilityHookup]
# @return [Utility]
def self.from_utility_hookup(utility_hookup)
new_from_slug(utility_hookup.utility_slug, utility_hookup: utility_hookup)
fetch(utility_hookup.utility_slug)
.from_utility_hookup(utility_hookup)
end

def self.new_from_slug(slug, attributes = {})
REGISTRY.fetch(slug.to_sym, NullUtility).new(attributes)
fetch(slug).new(attributes)
end

def self.fetch(slug)
REGISTRY.fetch(slug.to_sym, UtilityHookup)
end
end
4 changes: 4 additions & 0 deletions app/utilities/utility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ def form_template
def display_name
model_name.human.titleize
end

def self.from_utility_hookup(utility_hookup)
new(utility_hookup: utility_hookup)
end
end
4 changes: 2 additions & 2 deletions app/views/application/_select.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<%= form.label attribute %>
<%= form.select attribute, options, include_blank: include_blank %>
<%= render partial: "error", locals: { model: form.object, attribute: attribute } %>
<%= form.select attribute, options, include_blank: local_assigns.fetch(:include_blank, true) %>
<%= render partial: "error", locals: { model: form.object, attribute: attribute } %>
4 changes: 3 additions & 1 deletion app/views/spaces/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
<h3>Utility Hookups</h3>
<%- space.utility_hookups.each do |utility_hookup| %>
<%- if policy(utility_hookup).edit? %>
<%= render partial: "utility_hookups/form", locals: { utility_hookup: utility_hookup } %>
<%= render partial: "utility_hookups/form", locals: { utility_hookup: utility_hookup } %>
<%- end %>
<%- end %>

<%= link_to "Add a Utility", [:new, space, :utility_hookup] %>
</fieldset>
5 changes: 3 additions & 2 deletions app/views/utility_hookups/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<%= form_with(model: utility_hookup, url: space_utility_hookup_path(utility_hookup.space, utility_hookup), local: true) do |form| %>
<%= form_with(model: [utility_hookup.space, utility_hookup], local: true) do |form| %>
<%- form.object = form.object.polymorph %>
<%= form.hidden_field :utility_slug %>
<header>
<h4>
Expand All @@ -15,4 +16,4 @@
<%= form.submit "Save changes to #{utility_hookup.utility.display_name} ✔️" %>
<%- end %>
</footer>
<%- end %>
<%- end %>
5 changes: 5 additions & 0 deletions app/views/utility_hookups/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= form_with(model: [utility_hookup.space, utility_hookup], local: true) do |form| %>
<%= render "select", attribute: :utility_slug, options: Utilities::REGISTRY.keys, form: form %>
<%= render "text_field", attribute: :name, form: form %>
<%= form.submit %>
<%- end %>
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
end
end

resources :utility_hookups, only: %I[create edit update destroy index]
resources :utility_hookups

resources :memberships, only: %I[index show destroy]
end
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/spaces/utility_hookups_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
end
let(:utility_hookup) { FactoryBot.create(:utility_hookup, space: space) }

let(:guest) { nil }
let(:guest) { nil }
let(:neighbor) { FactoryBot.create(:person) }
let(:space_member) { space.members.first }

Expand Down

0 comments on commit c019ab1

Please sign in to comment.