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

Migrations, models and controllers for CSR and SSR #26

Merged
merged 15 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ Style/FrozenStringLiteralComment:
Style/PercentLiteralDelimiters:
Enabled: false

# Until today I have never needed to know created_at and updated_at. ¯\_(ツ)_/¯
# Let's only add them when we have a good reason.
Rails/CreateTableWithTimestamps:
Enabled: false

# Because warning asked to add these...
Lint/RaiseException:
Enabled: true
Expand All @@ -68,3 +73,5 @@ Style/HashTransformKeys:
Enabled: true
Style/HashTransformValues:
Enabled: true


9 changes: 7 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
class ApplicationController < ActionController::API
# To give all controllers the tools to be JSON API spec compliant.
# Give all controllers the methods for handling requests per JSON API spec.
include JsonApiController

# To allow controllers to set and read cookies.
# Allow controllers to set and read cookies.
include ActionController::Cookies

# Allow all controllers to log with a shorthand.
def log
Rails.logger
end
end
9 changes: 7 additions & 2 deletions app/controllers/concerns/json_api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,13 @@ def forbidden_filters
# allow_create
# end
#
def allow_create
resource = model_class.new(attributes_and_relationships)
def allow_create(controller_attributes)
hash = strong_attributes
.merge(strong_relationships)
.merge(controller_attributes)

resource = model_class.new(hash)

if resource.save!
json = serializer_class.new(resource).serializable_hash.to_json
render status: 201, json: json
Expand Down
35 changes: 35 additions & 0 deletions app/controllers/v1/admin/client_side_renders_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module V1
module Admin
class ClientSideRendersController < ApplicationController
def index
allow_index
end

def show
allow_show
end

def create
forbidden
end

def update
forbidden
end

def destroy
forbidden
end

private

def model_class
ClientSideRender
end

def serializer_class
V1::Admin::ClientSideRenderSerializer
end
end
end
end
35 changes: 35 additions & 0 deletions app/controllers/v1/admin/page_views_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module V1
module Admin
class PageViewsController < ApplicationController
def index
allow_index
end

def show
allow_show
end

def create
forbidden
end

def update
forbidden
end

def destroy
forbidden
end

private

def model_class
PageView
end

def serializer_class
V1::Admin::PageViewSerializer
end
end
end
end
35 changes: 35 additions & 0 deletions app/controllers/v1/admin/server_side_renders_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module V1
module Admin
class ServerSideRendersController < ApplicationController
def index
allow_index
end

def show
allow_show
end

def create
forbidden
end

def update
forbidden
end

def destroy
forbidden
end

private

def model_class
ServerSideRender
end

def serializer_class
V1::Admin::ServerSideRenderSerializer
end
end
end
end
49 changes: 49 additions & 0 deletions app/controllers/v1/admin/visits_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module V1
module Admin
class VisitsController < ApplicationController
def index
allow_index
end

def show
allow_show
end

def create
forbidden
end

def update
forbidden
end

def destroy
forbidden
end

private

def model_class
Visit
end

def serializer_class
V1::Admin::VisitSerializer
end

def permitted_filters
%i[
host
]
end

def permitted_includes
%i[
server_side_renders
client_side_renders
user
]
end
end
end
end
61 changes: 61 additions & 0 deletions app/controllers/v1/public/client_side_renders_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module V1
module Public
class ClientSideRendersController < ApplicationController
def index
forbidden
end

def show
forbidden
end

def create
allow_create(
{
id: params[:data][:id],
ip: request.remote_ip
}
)
end

def update
forbidden
end

def destroy
forbidden
end

private

def model_class
ClientSideRender
end

def serializer_class
V1::Public::ClientSideRenderSerializer
end

def creatable_attributes
%i[
host
path
referrer
user_agent
viewport_width
viewport_height
server_side_render_id
browser_session_id
user_id
]
end

def creatable_relationships
%[
visit
user
]
end
end
end
end
35 changes: 35 additions & 0 deletions app/controllers/v1/public/page_views_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module V1
module Public
class PageViewsController < ApplicationController
def index
allow_index
end

def show
allow_show
end

def create
forbidden
end

def update
forbidden
end

def destroy
forbidden
end

private

def model_class
PageView
end

def serializer_class
V1::Public::PageViewSerializer
end
end
end
end
55 changes: 55 additions & 0 deletions app/controllers/v1/public/server_side_renders_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module V1
module Public
class ServerSideRendersController < ApplicationController
def index
forbidden
end

def show
forbidden
end

def create
allow_create(
{
id: params[:data][:id],
ip: request.remote_ip
}
)
end

def update
forbidden
end

def destroy
forbidden
end

private

def model_class
ServerSideRender
end

def serializer_class
V1::Public::ServerSideRenderSerializer
end

def creatable_attributes
%i[
host
path
referrer
user_agent
]
end

def creatable_relationships
%[
visit
]
end
end
end
end
29 changes: 29 additions & 0 deletions app/controllers/v1/public_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
module V1
class PublicController < ApplicationController
before_action :create_visit

private

def create_visit
id = request.headers['Visit-ID']

return nil if id.nil?

record = Visit.find_by(id: id)

if record.nil?
Visit.create!(
id: id,
start_time: DateTime.current,
user_agent_ssr: request.headers['SSR-User-Agent']
)
log.debug '🌈 visit created'
elsif !record.user_agent? || !request.browser_width
Visit.update(
user_agent: request.user_agent,
browser_width: request.headers['Browser-Width'],
browser_height: request.headers['Browser-Height']
)
log.debug '🌳 visit updated'
else
log.debug '❌ doing nothing'
end
end
end
end
Loading
Loading