Rich Smith

Joined

910 Experience
0 Lessons Completed
1 Question Solved

Activity

Posted in Using Webhooks with Stripe Discussion

Thanks for getting back to me! I am still struggling with this :/

Edit: Got it working but it feels dirty...

Here's what I have:

/config/intializers/stripe.rb

...
StripeEvent.configure do |events|
  events.subscribe 'charge.succeeded', WebhookHandlers::StripeWebhookHandler.new
end

/app/services/webhook_handlers/stripe_webhook_handler.rb

module WebhookHandlers
  module StripeWebhookHandler
    class RecordCharges
      def call(event)
        ...
      end
    end
  end
end

Any chance you could point me in the right direction on wheather or not this is clean?

Posted in Using Webhooks with Stripe Discussion

Hey Chris,

I'm trying to figure out how to fix up my code and move the RecordCharges class out of my initializer and into a PlainOldRubyObject™ located in app/services/webhook_handlers/stripe_webhook_handler.rb but am running into an issue that because the StripeEvent is called in an initializer (config/initializers/stripe.rb), the rest of the app isn't loaded so it is not able to call the class. Any chance you could help me out?

Thanks,

Rich

Posted in Stripe Elements Javascript Discussion

Hey Chris,

Running into a small issue with Rails 5.2 and Content-Security-Policy issues with the stripe elements.

Any suggestions?

I tried poking around config > initializers > content_security_policy.rb but changes such as the following didn't do much.

# Define an application-wide content security policy
# For further information see the following documentation
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
Rails.application.config.content_security_policy do |policy|
  policy.connect_src :self, :https, 'http://localhost:3035', 'ws://localhost:3035' if Rails.env.development?
  # policy.default_src :self, :https
  # policy.font_src    :self, :https, :data
  # policy.img_src     :self, :https, :data
  # policy.object_src  :none
  policy.script_src  :self, :https, 'stripe.com'
  # policy.style_src   :self, :https

  #   # Specify URI for violation reports
  # policy.report_uri '/csp-violation-report-endpoint'
end

Posted in How do I apply a Pundit Policy to Index?

Formatted...

I'd like to have an index page show only the associated (from the parent, location) records, and allow only users that own those records to see the index.  I am really struggling with this concept of scopes and would appreciate any help.

Here's where I'm at:

Models

User
  has_one :business
  has_many :locations, :through => :business
end

Business
  belongs_to :user
  has_many :locations
end

Location
  extend FriendlyId
  belongs_to :business
  has_one :user, :through => :business
  has_many :sites, dependent: :destroy
  friendly_id :custom_url, use: :slugged
end

Site
  belongs_to :location
end


routes.rb


 resources :locations do
    resources :sites
  end


sites_controller.rb


class SitesController < ApplicationController
  before_action :set_site, only: [:show, :edit, :update, :destroy]
  before_action :set_location, only: [:new, :show, :edit, :index, :update, :destroy]
  
  
  def index
    authorize Site
    @sites = @location.sites.all
  end

  private
    def set_site
      @site = Site.find(params[:id])
    end
    def set_location
      @location = Location.friendly.find(params[:location_id])
    end
    def site_params
      params.require(:site).permit(:location_id, :site, :url, :review_site_id, :number_of_reviews, :average_rating, :extra_data)
    end
end
  

site_policy.rb



class SitePolicy < ApplicationPolicy
    class Scope
      attr_reader :user, :scope
  
      def initialize(user, scope)
        @user  = user
        @scope = scope
      end
  
      def resolve
        if user.has_role? :admin
          scope.all
        else
          scope.where(location.user)
        end
      end
    end

  def index? 
    return true if user.present? and user.has_role? :admin
  end
  ...



Any help or pointers at all would be super appreciated, I am really struggling wrapping my head around Pundit Scopes, but am keenly aware that I need them to get an index page to work. 

Posted in How do I apply a Pundit Policy to Index?

I'd like to have an index page show only the associated (from the parent, location) records, and allow only users that own those records to see the index.  I am really struggling with this concept of scopes and would appreciate any help.

Here's where I'm at:
# New Document
Here's where I'm at:

#### Models

```
User
  has_one :business
  has_many :locations, :through => :business
end

Business
  belongs_to :user
  has_many :locations
end

Location
  extend FriendlyId
  belongs_to :business
  has_one :user, :through => :business
  has_many :sites, dependent: :destroy
  friendly_id :custom_url, use: :slugged
end

Site
  belongs_to :location
end

```

#### routes.rb
```
 resources :locations do
    resources :sites
  end
```

#### sites_controller.rb
```
class SitesController < ApplicationController
  before_action :set_site, only: [:show, :edit, :update, :destroy]
  before_action :set_location, only: [:new, :show, :edit, :index, :update, :destroy]
  
  
  def index
    authorize Site
    @sites = @location.sites.all
  end

  private
    def set_site
      @site = Site.find(params[:id])
    end
    def set_location
      @location = Location.friendly.find(params[:location_id])
    end
    def site_params
      params.require(:site).permit(:location_id, :site, :url, :review_site_id, :number_of_reviews, :average_rating, :extra_data)
    end
end
  
  ```

#### site_policy.rb

```
class SitePolicy < ApplicationPolicy
    class Scope
      attr_reader :user, :scope
  
      def initialize(user, scope)
        @user  = user
        @scope = scope
      end
  
      def resolve
        if user.has_role? :admin
          scope.all
        else
          scope.where(location.user)
        end
      end
    end

  def index? 
    return true if user.present? and user.has_role? :admin
  end
  ...
```


Any help or pointers at all would be super appreciated, I am really struggling wrapping my head around Pundit Scopes, but am keenly aware that I need them to get an index page to work. 




Posted in Few Big Models or Many Smaller Namspaced models?

... I never really thought about the fact that my controllers don't have to map directly to models :/  That makes perfect sense. I suppose I will do a generic Page controller, then site(yelp, facebook, google, etc.) specific when required. 

Thanks for all your help Chris!

Posted in Few Big Models or Many Smaller Namspaced models?

Oh man, having a separate column with json to handle anything unique (which there really shouldn't be much of, if any) is brilliant! Just having a PORO handle any api stuff is perfect. I think that's exactly the route I will take.

Thanks so much for your help Chris! I really appreciate it. 

One last question, when I'm building out forms, what would be the best way to have page-specific forms? Just create different views for specific pages?

Posted in Few Big Models or Many Smaller Namspaced models?

Thanks Chris! Yea I am leaning towards separate models just because eventually I would like to have each site have different functions and am limited by the different sites having different API options.

Do you think something like this would work?

  • User has_one business
  • Business has_many :locations
  • Location has_one :facebook_page, through => :review_sites
  • Location has_one :google_page, through => :review_sites
  • Location has_one :yelp_page, through => :review_sites
  • ReviewSite belongs_to :location, has_one :yelp_page, has_one :google_page, has_one :facebook_page
  • FacebookPage belongs_to :review_site
  • GooglePage belongs_to :review_site
  • YelpPage belongs_to :review_site

I'd prefer not having a messy models folder though, is something like [this](http://blog.hasmanythrough.com/2008/5/6/a-simple-alternative-to-namespaced-models) still an option in Rails 5.2?

Posted in Few Big Models or Many Smaller Namspaced models?

Anyone able to give suggestions on model design? I'm building an app to help small businesses get more reviews across the web. Concept is simple, user registers a business, and their businesses location(s). They then send emails to their clients that contain a direct link to their locations review site (google my business, facebook review page, yelp etc.) Models so far: Users, Businesses, Locations. Businesses have many locations. The end goal is there will be a review_request model (which keeps track of the review requests for each location sent to the businesses clients) and a review_sites model - which would contain info on the locations review site (example site_name:facebook, site_domain:https://facebook.com, location_page: facebook.com/business-id-here.

My question is, should I keep all review sites in one big ugly model? Or should I namespace them and give each specific review site its own model? example models/review_sites/google_site.rb?

Posted in Best Method For User's Dashboard?

Hi, I am looking for advice on the best way to organize a dashboard to show the user various information from across different models. 

I was considering just generating a DasboardsController, but that doesn't seem very rails-y - for starts because of the fact that it is plural when in reality, it just needs to be DashboardController#index.


My app is set up in the following way:

Users
    |
    Businesses
        |
        Locations
         |  |
         |  Requests
        Payments
        OtherThings

I considered simply adding businesses#dashboard, but that seems wrong. I am trying to display chunks of info from children of the business model.

Any suggestions on design pattern here?

Cheers!
logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2020 GoRails, LLC. All rights reserved.