Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stripe: Sprout Initial Stripe Utility #1003

Merged
merged 7 commits into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ok for now, but I can imagine wanting to have more than one Stripe account associated with a space in the future. But maybe the hookup itself will support multiple API keys. We'll cross that bridge when we get to it.

Copy link
Member Author

@zspencer zspencer Dec 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is validating the name given to the utility to disambiguate it against different utilities on the same space. I.e. Stripe Account - Jim's Beans vs Stripe Account - Jane's Jams

Maybe we should rename the field to label? Does that feel more clear than name?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh 🤦🏼‍♀️ Bad code reviewer, no donut for me.


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
zspencer marked this conversation as resolved.
Show resolved Hide resolved
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
zspencer marked this conversation as resolved.
Show resolved Hide resolved
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))
zspencer marked this conversation as resolved.
Show resolved Hide resolved
&.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
zspencer marked this conversation as resolved.
Show resolved Hide resolved
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
zspencer marked this conversation as resolved.
Show resolved Hide resolved
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
zspencer marked this conversation as resolved.
Show resolved Hide resolved
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