Module: Spree::Admin::BaseControllerDecorator

Defined in:
app/controllers/spree/admin/base_controller_decorator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



4
5
6
# File 'app/controllers/spree/admin/base_controller_decorator.rb', line 4

def self.prepended(base)
  base.before_action :redirect_to_billing, if: -> { Spree::Store.default.code.include?('billing') }
end

Instance Method Details

#admin_oauth_tokenObject

override: even in view actions, head.html.erb still need admin_oauth_token which may create new token.



17
18
19
20
21
# File 'app/controllers/spree/admin/base_controller_decorator.rb', line 17

def admin_oauth_token
  ActiveRecord::Base.connected_to(role: :writing) do
    super
  end
end

#authorize!(*args) ⇒ Object

even db in read only mode, authorizor still need to run Warden callbacks which will write to db this include update tracked fields such as sign_in_count, last_sign_in_at, etc.



10
11
12
13
14
# File 'app/controllers/spree/admin/base_controller_decorator.rb', line 10

def authorize!(*args)
  ActiveRecord::Base.connected_to(role: :writing) do
    super
  end
end

#invalidate_api_cachesObject

POST



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/spree/admin/base_controller_decorator.rb', line 35

def invalidate_api_caches # rubocop:disable Metrics/CyclomaticComplexity
  if params[:model].present?
    model = case params[:model]
            when 'SpreeCmCommissioner::HomepageSection'
              SpreeCmCommissioner::HomepageSection
            when 'SpreeCmCommissioner::HomepageBackground'
              SpreeCmCommissioner::HomepageBackground
            when 'Spree::Menu'
              Spree::Menu
            when 'Spree::Taxon'
              Spree::Taxon
            when 'Spree::Product'
              Spree::Product
            else
              flash[:error] = 'Invalid model provided' # rubocop:disable Rails/I18nLocaleTexts
              redirect_back fallback_location: admin_root_path and return
            end

    model.find_each(&:touch)
    api_patterns_map = {
      SpreeCmCommissioner::HomepageSection => '/api/v2/storefront/homepage/*',
      SpreeCmCommissioner::HomepageBackground => '/api/v2/storefront/homepage/*',
      Spree::Menu => '/api/v2/storefront/menus*',
      Spree::Taxon => '/api/v2/storefront/taxons*',
      Spree::Product => '/api/v2/storefront/products*'
    }
    api_patterns = api_patterns_map[model]

    if api_patterns.is_a?(Array)
      api_patterns.each { |pattern| SpreeCmCommissioner::InvalidateCacheRequestJob.perform_later(pattern) }
    elsif api_patterns.is_a?(String)
      SpreeCmCommissioner::InvalidateCacheRequestJob.perform_later(api_patterns)
    elsif params[:api_pattern].present?
      SpreeCmCommissioner::InvalidateCacheRequestJob.perform_later(params[:api_pattern])
    end
  end
  redirect_back fallback_location: admin_root_path
end

#parse_date(date, format: nil) ⇒ Object



28
29
30
31
32
# File 'app/controllers/spree/admin/base_controller_decorator.rb', line 28

def parse_date(date, format: nil)
  parse_date!(date, format)
rescue Date::Error
  nil
end

#parse_date!(date, format: nil) ⇒ Object

2023-07-31



24
25
26
# File 'app/controllers/spree/admin/base_controller_decorator.rb', line 24

def parse_date!(date, format: nil)
  Date.strptime(date.to_s, format || '%Y-%m-%d')
end