Module: Bp3::Core::Actions

Extended by:
ActiveSupport::Concern
Defined in:
lib/bp3/core/actions.rb

Overview

rubocop:disable Metrics/ModuleLength

Instance Method Summary collapse

Instance Method Details

#check_siteObject (private)



86
87
88
89
# File 'lib/bp3/core/actions.rb', line 86

def check_site
  current_site || create_site
  grsc.current_site = current_site
end

#check_site_modeObject (private)



194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/bp3/core/actions.rb', line 194

def check_site_mode
  return unless current_site.sites_modes.enabled.timely.any?

  current_site.sites_modes.enabled.timely.each do |mode|
    next unless mode.name == 'maintenance'

    @resource = @sites_mode = mode
    @content = @resource.content
    @content_format = @resource.content_format
    render template: 'sites/modes/maintenance'
    break
  end
end

#check_tenantObject (private)



129
130
131
132
# File 'lib/bp3/core/actions.rb', line 129

def check_tenant
  current_tenant || create_tenant
  grsc.current_tenant = current_tenant
end

#check_workspaceObject (private)

TODO: cache the workspace/regex list



102
103
104
105
# File 'lib/bp3/core/actions.rb', line 102

def check_workspace
  current_workspace || create_workspace
  grsc.current_workspace = current_workspace
end

#create_request_recordObject (private)



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bp3/core/actions.rb', line 56

def create_request_record
  return if do_not_track
  return unless create_request_record_is_on?

  # Apparently, we can receive multiple requests with the same request_id
  # TODO: relax the uniqueness constraint instead of re-using the existing record
  @global_request = Inbound::Request.find_by(id: request.request_id)
  return @global_request if @global_request

  @global_request = Inbound::Request.create!(id: global_rqid,
                                             url: request.url,
                                             ip_address: request.ip,
                                             referer: request.referer,
                                             inbound_user_agent: @global_user_agent,
                                             sites_site: current_site)
end

#create_siteObject (private)



97
98
99
# File 'lib/bp3/core/actions.rb', line 97

def create_site
  @current_site = Sites::Site.create!(request_domain:)
end

#create_tenantObject (private)



140
141
142
# File 'lib/bp3/core/actions.rb', line 140

def create_tenant
  @current_tenant = Tenant.create!(sites_site: current_site, tenantable:)
end

#create_workspaceObject (private)

TODO: move (some of) this into Workspace



121
122
123
124
125
126
127
# File 'lib/bp3/core/actions.rb', line 121

def create_workspace
  workspace_type = Workspaces::WorkspaceType
                   .find_or_create_by!(sites_site: current_site, name: 'default')
  @current_workspace = Workspaces::Workspace.create!(sites_site: current_site,
                                                     name: 'default', match_pattern: '\A.*\z',
                                                     workspaces_workspace_type: workspace_type)
end

#current_siteObject (private)



91
92
93
94
95
# File 'lib/bp3/core/actions.rb', line 91

def current_site
  return @current_site if @current_site

  @current_site = request.request_site
end

#current_tenantObject (private)



134
135
136
137
138
# File 'lib/bp3/core/actions.rb', line 134

def current_tenant
  return @current_tenant if @current_tenant

  @current_tenant = current_site.tenants.find_by(tenantable:)
end

#current_workspaceObject (private)

TODO: move (some of) this into Workspace



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/bp3/core/actions.rb', line 108

def current_workspace
  return @current_workspace if @current_workspace
  return nil if current_site.nil?

  current_site.workspaces.find_each do |workspace|
    if workspace.has_pattern_match?(request.subdomain)
      @current_workspace = workspace
      return @current_workspace
    end
  end
end

#either_siteObject (private)



190
191
192
# File 'lib/bp3/core/actions.rb', line 190

def either_site
  grsc.either_site
end

#find_or_create_user_agentObject (private)



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bp3/core/actions.rb', line 73

def find_or_create_user_agent
  return if do_not_track

  user_agent = request.user_agent.presence || 'user-agent-is-blank'
  @global_user_agent = Inbound::UserAgent.find_or_create_by!(user_agent:)
rescue ActiveRecord::RecordInvalid
  # probably hit a race condition. Try again and log
  @global_user_agent = Inbound::UserAgent.find_by!(user_agent:)
  message = "User agent already existed (#{@global_user_agent.id}/#{user_agent})"
  log_warn(key: 'find_or_create_user_agent', message:)
  @global_user_agent
end

#global_rqidObject (private)



52
53
54
# File 'lib/bp3/core/actions.rb', line 52

def global_rqid
  @global_rqid ||= request.request_id || SecureRandom.uuid
end

#grscObject (private)



208
209
210
# File 'lib/bp3/core/actions.rb', line 208

def grsc
  @grsc ||= Bp3::Core::Rqid.global_request_state_class
end

#info_for_paper_trailObject (private)



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/bp3/core/actions.rb', line 164

def info_for_paper_trail
  state = grsc.new
  {
    rqid: global_rqid,
    sites_site_id: state.current_site&.id,
    tenant_id: state.current_tenant&.id,
    workspaces_workspace_id: state.current_workspace&.id,
    root_id: state.current_root&.id,
    sites_admin_id: state.current_admin&.id,
    users_user_id: state.current_user&.id
  }
end

#pundit_userObject (private)



186
187
188
# File 'lib/bp3/core/actions.rb', line 186

def pundit_user
  UserContext.new
end

#request_domainObject (private)



148
149
150
# File 'lib/bp3/core/actions.rb', line 148

def request_domain
  @request_domain ||= DomainExtractor.extract_domain(request_host) || request.domain || request_host
end

#request_hostObject (private)



152
153
154
# File 'lib/bp3/core/actions.rb', line 152

def request_host
  request.normalized_host
end

#request_subdomainObject (private)



156
157
158
159
160
161
162
# File 'lib/bp3/core/actions.rb', line 156

def request_subdomain
  return @request_subdomain if @request_subdomain

  subdomain = request_host.gsub(/#{request_domain}\z/, '')
  subdomain = subdomain[0..-2] if subdomain.ends_with?('.')
  @request_subdomain = subdomain
end

#set_global_request_stateObject (private)



39
40
41
42
43
44
45
46
# File 'lib/bp3/core/actions.rb', line 39

def set_global_request_state
  grsc.inbound_request = @global_request
  grsc.current_user = current_user
  grsc.current_admin = current_admin
  grsc.current_root = current_root
  grsc.locale = I18n.locale
  grsc.view_context = view_context
end

#set_ransack_auth_objectObject (private)



177
178
179
# File 'lib/bp3/core/actions.rb', line 177

def set_ransack_auth_object
  current_root || current_admin || current_user
end

#set_rqidObject (private)



48
49
50
# File 'lib/bp3/core/actions.rb', line 48

def set_rqid
  grsc.request_id = global_rqid
end

#tenantableObject (private)



144
145
146
# File 'lib/bp3/core/actions.rb', line 144

def tenantable
  @tenantable ||= multi_tenant_is_on? ? current_workspace : current_site
end

#user_for_paper_trailObject (private)

used by set_paper_trail_whodunnit



182
183
184
# File 'lib/bp3/core/actions.rb', line 182

def user_for_paper_trail
  current_root || current_admin || current_user || current_visitor || 'guest'
end