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

Add filters to sources #142

Merged
merged 9 commits into from
Aug 16, 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
37 changes: 37 additions & 0 deletions app/controllers/concerns/discourse_events/filters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true
module DiscourseEvents
module Filters
def valid_filters
end

def save_filters
return unless valid_filters.present?
@errors ||= []
saved_ids = []

valid_filters.each do |f|
params = f.slice(:query_column, :query_operator, :query_value)

if f[:id] === "new"
filter =
DiscourseEvents::Filter.create(
model_id: @model.id,
model_type: @model.class.name,
**params,
)
else
filter = @model.filters.update(f[:id].to_i, params)
end

if filter.errors.any?
@errors << filter.errors
raise ActiveRecord::Rollback
end

saved_ids << filter.id
end

@model.filters.where.not(id: saved_ids).destroy_all
end
end
end
65 changes: 21 additions & 44 deletions app/controllers/discourse_events/connection_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

module DiscourseEvents
class ConnectionController < AdminController
include DiscourseEvents::Filters

def index
connections = Connection.includes(:user)

render_json_dump(
connections: serialize_data(connections, ConnectionSerializer, root: false),
sources: serialize_data(Source.all, SourceSerializer, root: false),
clients: DiscourseEvents::Connection.available_clients,
)
end

def create
create_or_update

if @errors.blank?
render_serialized(@connection, ConnectionSerializer, root: "connection")
render_serialized(@model, ConnectionSerializer, root: "connection")
else
render json: failed_json.merge(errors: @errors.map(&:full_messages).flatten), status: 400
end
Expand All @@ -26,7 +27,7 @@ def update
create_or_update

if @errors.blank?
render_serialized(@connection, ConnectionSerializer, root: "connection")
render_serialized(@model, ConnectionSerializer, root: "connection")
else
render json: failed_json.merge(errors: @errors.map(&:full_messages).flatten), status: 400
end
Expand Down Expand Up @@ -59,7 +60,6 @@ def connection_params
:user_id,
:category_id,
:source_id,
:client,
filters: %i[id query_column query_operator query_value],
)
.to_h
Expand All @@ -77,57 +77,34 @@ def create_or_update

ActiveRecord::Base.transaction do
if action_name === "create"
@connection =
Connection.create(connection_params.slice(:user_id, :category_id, :source_id, :client))
@model = Connection.create(connection_params.slice(:user_id, :category_id, :source_id))
else
@connection =
@model =
Connection.update(
params[:id],
connection_params.slice(:user_id, :category_id, :source_id, :client),
connection_params.slice(:user_id, :category_id, :source_id),
)
end

if @connection.errors.any?
@errors << @connection.errors
if @model.errors.any?
@errors << @model.errors
raise ActiveRecord::Rollback
end

if connection_params[:filters].present?
valid_filters =
connection_params[:filters].select do |filter|
has_keys =
%i[id query_column query_operator query_value].all? { |key| filter.key?(key) }
has_values = filter.values.all?(&:present?)
has_keys && has_values
end

saved_ids = []

valid_filters.each do |f|
params = f.slice(:query_column, :query_operator, :query_value)

if f[:id] === "new"
filter =
DiscourseEvents::Filter.create(
model_id: @connection.id,
model_type: "DiscourseEvents::Connection",
**params,
)
else
filter = @connection.filters.update(f[:id].to_i, params)
end

if filter.errors.any?
@errors << filter.errors
raise ActiveRecord::Rollback
end

saved_ids << filter.id
end
save_filters
end
end

@connection.filters.where.not(id: saved_ids).destroy_all
def valid_filters
@valid_filters ||=
begin
(connection_params[:filters] || []).select do |filter|
has_keys =
%i[id query_column query_operator query_value].all? { |key| filter.key?(key) }
has_values = filter.values.all?(&:present?)
has_keys && has_values
end
end
end
end
end
end
84 changes: 66 additions & 18 deletions app/controllers/discourse_events/source_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module DiscourseEvents
class SourceController < AdminController
include DiscourseEvents::Filters

def index
render_json_dump(
sources: serialize_data(Source.all, SourceSerializer, root: false),
Expand All @@ -10,22 +12,51 @@ def index
end

def create
source = Source.create(source_params)
@errors = []

ActiveRecord::Base.transaction do
@model =
Source.create(
source_params.slice(:name, :provider_id, :status, :taxonomy, :source_options),
)

if @model.errors.any?
@errors += @model.errors.full_messages
raise ActiveRecord::Rollback
end

if source.errors.blank?
render_serialized(source, SourceSerializer, root: "source")
save_filters
end

if @errors.blank?
render_serialized(@model, SourceSerializer, root: "source")
else
render json: failed_json.merge(errors: source.errors.full_messages), status: 400
render json: failed_json.merge(errors: @errors), status: 400
end
end

def update
source = Source.update(params[:id], source_params)
@errors = []

ActiveRecord::Base.transaction do
@model =
Source.update(
params[:id],
source_params.slice(:name, :provider_id, :status, :taxonomy, :source_options),
)

if source.errors.blank?
render_serialized(source, SourceSerializer, root: "source")
if @model.errors.any?
@errors += @model.errors.full_messages
raise ActiveRecord::Rollback
end

save_filters
end

if @errors.blank?
render_serialized(@model, SourceSerializer, root: "source")
else
render json: failed_json.merge(errors: source.errors.full_messages), status: 400
render json: failed_json.merge(errors: @errors), status: 400
end
end

Expand All @@ -49,16 +80,33 @@ def destroy
protected

def source_params
params.require(:source).permit(
:name,
:provider_id,
:from_time,
:to_time,
:status,
:taxonomy,
source_options: {
},
)
@source_params ||=
begin
params
.require(:source)
.permit(
:name,
:provider_id,
:status,
:taxonomy,
source_options: {
},
filters: %i[id query_column query_operator query_value],
)
.to_h
end
end

def valid_filters
@valid_filters ||=
begin
(source_params[:filters] || []).select do |filter|
has_keys =
%i[id query_column query_operator query_value].all? { |key| filter.key?(key) }
has_values = filter.values.all?(&:present?)
has_keys && has_values
end
end
end
end
end
7 changes: 3 additions & 4 deletions app/models/discourse_events/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def self.client_names
dependent: :destroy
has_many :events, through: :event_connections, source: :event
has_many :filters,
-> { where(model_type: "DiscourseEvents::Connection") },
foreign_key: "model_id",
class_name: "DiscourseEvents::Filter",
dependent: :destroy
Expand Down Expand Up @@ -49,17 +50,15 @@ def self.plugins
#
# id :bigint not null, primary key
# user_id :bigint
# category_id :bigint
# source_id :bigint not null
# category_id :bigint
# client :string
# from_time :datetime
# to_time :datetime
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# events_connections_category_source (category_id,source_id) UNIQUE
# events_source_category_connection_index (source_id,category_id) UNIQUE
# index_discourse_events_connections_on_category_id (category_id)
# index_discourse_events_connections_on_source_id (source_id)
# index_discourse_events_connections_on_user_id (user_id)
Expand Down
6 changes: 3 additions & 3 deletions app/models/discourse_events/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ class Event < ActiveRecord::Base
# status :string default("published")
# taxonomy :string
# url :string
# series_id :string
# occurrence_id :string
# source_id :bigint
# provider_id :bigint
# created_at :datetime not null
# updated_at :datetime not null
# series_id :string
# occurrence_id :string
#
# Indexes
#
# discourse_events_event_id_index (uid,provider_id) UNIQUE
# index_discourse_events_events_on_provider_id (provider_id)
# index_discourse_events_events_on_source_id (source_id)
# event_id_index (uid,provider_id) UNIQUE
#
# Foreign Keys
#
Expand Down
3 changes: 2 additions & 1 deletion app/models/discourse_events/event_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ class EventConnection < ActiveRecord::Base
# connection_id :bigint not null
# topic_id :bigint
# post_id :bigint
# series_id :string
# client :string
# created_at :datetime not null
# updated_at :datetime not null
# series_id :string
#
# Indexes
#
Expand Down
Loading
Loading