Skip to content

Commit

Permalink
Autoload resource classes
Browse files Browse the repository at this point in the history
Implements autoloading on Active Resources classes so they don't load
early unnecessarily. This improves load times for Rails applications,
especially in instances where they are not used.
  • Loading branch information
gmcgibbon committed Jan 3, 2025
1 parent 97eec35 commit 56b883e
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/active_resource/json_errors.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
require 'active_resource/base'
require 'active_model/errors'

module ActiveResource
class Errors < ActiveModel::Errors
Expand Down
44 changes: 22 additions & 22 deletions lib/shopify_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,40 @@
require 'digest/md5'
require 'base64'
require 'active_resource/detailed_log_subscriber'
require 'shopify_api/limits'
require 'shopify_api/api_version'
require 'shopify_api/meta'
require 'active_resource/json_errors'
require 'shopify_api/paginated_collection'
require 'shopify_api/disable_prefix_check'

if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.6")
puts("\nshopify_api: NOTE: Support for Ruby #{RUBY_VERSION} will be dropped in the next major release. Please update to Ruby 2.6 or newer before updating this gem.\n\n")
end

module ShopifyAPI
extend ActiveSupport::Autoload

require 'shopify_api/limits'
require 'shopify_api/api_version'
require 'shopify_api/paginated_collection'
require 'shopify_api/disable_prefix_check'
require 'shopify_api/events'
require 'shopify_api/metafields'
require 'shopify_api/countable'
require 'shopify_api/resources'
require 'shopify_api/session'
require 'shopify_api/hmac_params'
require 'shopify_api/api_access'
require 'shopify_api/message_enricher'
require 'shopify_api/connection'
require 'shopify_api/pagination_link_headers'
require 'shopify_api/graphql'

eager_autoload do
autoload(:Meta)
end

include Limits
end

require 'shopify_api/events'
require 'shopify_api/metafields'
require 'shopify_api/countable'
require 'shopify_api/resources'
require 'shopify_api/session'
require 'shopify_api/hmac_params'
require 'shopify_api/api_access'
require 'shopify_api/message_enricher'
require 'shopify_api/connection'
require 'shopify_api/pagination_link_headers'
require 'shopify_api/graphql'
require 'shopify_api/graphql/railtie' if defined?(Rails)

if ShopifyAPI::Base.respond_to?(:connection_class)
ShopifyAPI::Base.connection_class = ShopifyAPI::Connection
else
require 'active_resource/connection_ext'
end

if ENV["SHOPIFY_LOG_PATH"]
ActiveResource::Base.logger = Logger.new(ENV["SHOPIFY_LOG_PATH"])
ActiveResource::DetailedLogSubscriber.attach_to(:active_resource_detailed)
Expand Down
3 changes: 3 additions & 0 deletions lib/shopify_api/api_version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# frozen_string_literal: true

require "active_support/core_ext/hash"

module ShopifyAPI
class ApiVersion
class UnknownVersion < StandardError; end
Expand Down
2 changes: 2 additions & 0 deletions lib/shopify_api/graphql/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
module ShopifyAPI
module GraphQL
class Railtie < Rails::Railtie
config.eager_load_namespaces << ShopifyAPI

initializer 'shopify_api.initialize_graphql_clients' do |app|
ShopifyAPI::GraphQL.schema_location = app.root.join('db', ShopifyAPI::GraphQL.schema_location)
ShopifyAPI::GraphQL.initialize_clients
Expand Down
100 changes: 97 additions & 3 deletions lib/shopify_api/resources.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,98 @@
# frozen_string_literal: true
require 'shopify_api/resources/base'
require 'shopify_api/resources/array_base'
Dir.glob("#{File.dirname(__FILE__)}/resources/*").each { |file| require(file) }

module ShopifyAPI
eager_autoload do
autoload_under('resources') do
autoload(:Base)
autoload(:ArrayBase)
autoload(:AbandonedCheckout)
autoload(:AccessScope)
autoload(:AccessToken)
autoload(:Address)
autoload(:Announcement)
autoload(:ApiPermission)
autoload(:ApplicationCharge)
autoload(:ApplicationCredit)
autoload(:ArrayBase)
autoload(:Article)
autoload(:Asset)
autoload(:AssignedFulfillmentOrder)
autoload(:Base)
autoload(:BillingAddress)
autoload(:Blog)
autoload(:CarrierService)
autoload(:Cart)
autoload(:Checkout)
autoload(:Collect)
autoload(:Collection)
autoload(:CollectionListing)
autoload(:CollectionPublication)
autoload(:Comment)
autoload(:Country)
autoload(:Currency)
autoload(:CustomCollection)
autoload(:Customer)
autoload(:CustomerGroup)
autoload(:CustomerInvite)
autoload(:CustomerSavedSearch)
autoload(:DiscountCode)
autoload(:DiscountCodeBatch)
autoload(:DraftOrder)
autoload(:DraftOrderInvoice)
autoload(:Event)
autoload(:Fulfillment)
autoload(:FulfillmentEvent)
autoload(:FulfillmentOrder)
autoload(:FulfillmentOrderLocationsForMove)
autoload(:FulfillmentRequest)
autoload(:FulfillmentService)
autoload(:FulfillmentV2)
autoload(:GiftCard)
autoload(:Image)
autoload(:InventoryItem)
autoload(:InventoryLevel)
autoload(:LineItem)
autoload(:Location)
autoload(:MarketingEvent)
autoload(:Metafield)
autoload(:NoteAttribute)
autoload(:Option)
autoload(:Order)
autoload(:OrderRisk)
autoload(:Page)
autoload(:Payment)
autoload(:PaymentDetails)
autoload(:Policy)
autoload(:PriceRule)
autoload(:Product)
autoload(:ProductListing)
autoload(:ProductPublication)
autoload(:Province)
autoload(:Publication)
autoload(:Receipt)
autoload(:RecurringApplicationCharge)
autoload(:Redirect)
autoload(:Refund)
autoload(:Report)
autoload(:ResourceFeedback)
autoload(:Rule)
autoload(:ScriptTag)
autoload(:ShippingAddress)
autoload(:ShippingLine)
autoload(:ShippingRate)
autoload(:ShippingZone)
autoload(:Shop)
autoload(:SmartCollection)
autoload(:StorefrontAccessToken)
autoload(:TaxLine)
autoload(:TaxService)
autoload(:TenderTransaction)
autoload(:Theme)
autoload(:Transaction)
autoload(:UsageCharge)
autoload(:User)
autoload(:Variant)
autoload(:Webhook)
end
end
end
7 changes: 7 additions & 0 deletions lib/shopify_api/resources/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,10 @@ def only_id
end
end
end


if ShopifyAPI::Base.respond_to?(:connection_class)
ShopifyAPI::Base.connection_class = ShopifyAPI::Connection
else
require 'active_resource/connection_ext'
end
3 changes: 0 additions & 3 deletions lib/shopify_api/resources/ping.rb

This file was deleted.

0 comments on commit 56b883e

Please sign in to comment.