Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Move tableless concern to be a base model based on ActiveModel::Model

Klaus Zanders250288c66ab8
diff --git a/app/models/action.rb b/app/models/action.rb
index 0ae3927bf26..d1c98b84045 100644
--- a/app/models/action.rb
+++ b/app/models/action.rb
@@ -26,16 +26,13 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Action < ApplicationRecord
- include Tableless
+class Action < TablelessModel
include Scopes::Scoped
scopes :default
- default_scope { default }
-
- attribute :id, :text, default: nil
- attribute :permission, :text, default: nil
+ attribute :id, :string, default: nil
+ attribute :permission, :string, default: nil
attribute :global, :boolean, default: false
- attribute :module, :text, default: false
+ attribute :module, :string, default: false
end
diff --git a/app/models/capability.rb b/app/models/capability.rb
index 1d7c9729835..40b50ce2ced 100644
--- a/app/models/capability.rb
+++ b/app/models/capability.rb
@@ -26,18 +26,30 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Capability < ApplicationRecord
- include Tableless
+class Capability < TablelessModel
include Scopes::Scoped
scopes :default
- default_scope { default }
+ def context
+ @context ||= Project.find(context_id)
+ end
- belongs_to :context, class_name: "Project"
- belongs_to :principal
+ def context=(project)
+ @context = project
+ @context_id = project.id
+ end
- attribute :action, :text, default: nil
+ def principal
+ @principal ||= User.find(principal_id)
+ end
+
+ def principal=(principal)
+ @principal = principal
+ @principal_id = principal.id
+ end
+
+ attribute :action, :string, default: nil
attribute :context_id, :integer, default: nil
attribute :principal_id, :integer, default: nil
end
diff --git a/app/models/day.rb b/app/models/day.rb
index 6c006389c79..035ee438baf 100644
--- a/app/models/day.rb
+++ b/app/models/day.rb
@@ -26,62 +26,45 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Day < ApplicationRecord
- include Tableless
-
- has_many :non_working_days,
- inverse_of: false,
- class_name: "NonWorkingDay",
- foreign_key: :date,
- primary_key: :date,
- dependent: nil
-
+class Day < TablelessModel
+ attribute :id, :integer, default: nil
attribute :date, :date, default: nil
attribute :day_of_week, :integer, default: nil
- attribute :working, :boolean, default: "t"
+ attribute :working, :boolean, default: true
delegate :name, to: :week_day, allow_nil: true
- scope :working, -> { where(working: true) }
-
- def self.default_scope
- today = Time.zone.today
- from = today.at_beginning_of_month
- to = today.next_month.at_end_of_month
- from_range(from:, to:)
- .includes(:non_working_days)
- .order("days.id")
+ def non_working_days
+ @non_working_days ||= NonWorkingDay.where(date: date)
end
- def self.from_range(from:, to:)
- from(Arel.sql(from_sql(from:, to:)))
- end
+ class << self
+ def for_this_month
+ today = Time.zone.today
+ from = today.at_beginning_of_month
+ to = today.next_month.at_end_of_month
- def self.from_sql(from:, to:)
- from = from.to_date
- to = to.to_date
- <<~SQL.squish
- (SELECT
- to_char(dd, 'YYYYMMDD')::integer id,
- date_trunc('day', dd)::date date,
- extract(isodow from dd) day_of_week,
- (COALESCE(POSITION(extract(isodow from dd)::text IN settings.value) > 0, TRUE)
- AND non_working_days.id IS NULL)::bool working
- FROM
- generate_series( '#{from}'::timestamp,
- '#{to}'::timestamp,
- '1 day'::interval) dd
- LEFT JOIN settings
- ON settings.name = 'working_days'
- LEFT JOIN non_working_days
- ON dd = non_working_days.date
- ) days
- SQL
- end
+ from_range(from: from, to: to)
+ end
+
+ def from_range(from:, to:)
+ range = from.to_date..to.to_date
+ non_working_days = NonWorkingDay.where(date: range).pluck(:date)
+
+ range.map do |date|
+ new(
+ id: date.strftime("YYYYMMDD").to_i,
+ date: date,
+ day_of_week: date.wday,
+ working: date.wday.in?(Setting.working_days) && non_working_days.exclude?(date)
+ )
+ end
+ end
- def self.last_working
- # Look up only from 8 days ago, because the Setting.working_days must have at least 1 working weekday.
- from_range(from: 8.days.ago, to: Time.zone.yesterday).where(working: true).last
+ def last_working
+ # Look up only from 8 days ago, because the Setting.working_days must have at least 1 working weekday.
+ from_range(from: 8.days.ago, to: Time.zone.yesterday).reverse.find(&:working)
+ end
end
def week_day
diff --git a/app/models/queries/days/day_query.rb b/app/models/queries/days/day_query.rb
index 38db47af417..74b350c4025 100644
--- a/app/models/queries/days/day_query.rb
+++ b/app/models/queries/days/day_query.rb
@@ -35,7 +35,7 @@ class Queries::Days::DayQuery
end
def default_scope
- Day.default_scope
+ Day.for_this_month
end
##
@@ -48,8 +48,4 @@ class Queries::Days::DayQuery
scope = scope.from(from_clause_filter.from) if from_clause_filter
scope
end
-
- def results
- super.reorder(date: :asc)
- end
end
diff --git a/app/models/queries/days/filters/dates_interval_filter.rb b/app/models/queries/days/filters/dates_interval_filter.rb
index 54ea13a022b..709cc2142c0 100644
--- a/app/models/queries/days/filters/dates_interval_filter.rb
+++ b/app/models/queries/days/filters/dates_interval_filter.rb
@@ -49,7 +49,7 @@ class Queries::Days::Filters::DatesIntervalFilter < Queries::Days::Filters::DayF
to = from.next_month.at_end_of_month
end
- model.from_sql(from:, to:)
+ model.from_range(from:, to:)
end
def type_strategy
@@ -57,6 +57,6 @@ class Queries::Days::Filters::DatesIntervalFilter < Queries::Days::Filters::DayF
end
def connection
- ActiveRecord::Base::connection
+ nil
end
end
diff --git a/app/models/concerns/tableless.rb b/app/models/tableless_model.rb
similarity index 66%
rename from app/models/concerns/tableless.rb
rename to app/models/tableless_model.rb
index 70224951bac..270e1bb1081 100644
--- a/app/models/concerns/tableless.rb
+++ b/app/models/tableless_model.rb
@@ -26,8 +26,9 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-module Tableless
- extend ActiveSupport::Concern
+class TablelessModel
+ include ActiveModel::Model
+ include ActiveModel::Attributes
def persisted?
false
@@ -36,26 +37,4 @@ module Tableless
def readonly?
true
end
-
- class_methods do
- def attribute_names
- @attribute_names ||= attribute_types.keys
- end
-
- def load_schema!
- @columns_hash ||= Hash.new
-
- # From active_record/attributes.rb
- attributes_to_define_after_schema_loads.each do |name, (type, default)|
- if type.is_a?(Symbol)
- type = ActiveRecord::Type.lookup(type, default)
- end
-
- define_attribute(name, type, default:)
-
- # Improve Model#inspect output
- @columns_hash[name.to_s] = ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default)
- end
- end
- end
end