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

fix tableless models

Klaus Zanders478519db1427
diff --git a/app/models/action.rb b/app/models/action.rb
index d1c98b84045..2d5e121340e 100644
--- a/app/models/action.rb
+++ b/app/models/action.rb
@@ -26,13 +26,16 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Action < TablelessModel
+class Action < ApplicationRecord
+ include Tableless
include Scopes::Scoped
scopes :default
- attribute :id, :string, default: nil
- attribute :permission, :string, default: nil
- attribute :global, :boolean, default: false
- attribute :module, :string, default: false
+ default_scope { default }
+
+ define_attribute :id, ActiveRecord::Type.lookup(:text), default: nil
+ define_attribute :permission, ActiveRecord::Type.lookup(:text), default: nil
+ define_attribute :global, ActiveRecord::Type.lookup(:boolean), default: false
+ define_attribute :module, ActiveRecord::Type.lookup(:text), default: false
end
diff --git a/app/models/capability.rb b/app/models/capability.rb
index 40b50ce2ced..a163cd6044a 100644
--- a/app/models/capability.rb
+++ b/app/models/capability.rb
@@ -26,30 +26,18 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Capability < TablelessModel
+class Capability < ApplicationRecord
+ include Tableless
include Scopes::Scoped
scopes :default
- def context
- @context ||= Project.find(context_id)
- end
+ default_scope { default }
- def context=(project)
- @context = project
- @context_id = project.id
- end
+ belongs_to :context, class_name: "Project"
+ belongs_to :principal
- 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
+ define_attribute :action, ActiveRecord::Type.lookup(:text), default: nil
+ define_attribute :context_id, ActiveRecord::Type.lookup(:integer), default: nil
+ define_attribute :principal_id, ActiveRecord::Type.lookup(:integer), default: nil
end
diff --git a/app/models/tableless_model.rb b/app/models/concerns/tableless.rb
similarity index 87%
rename from app/models/tableless_model.rb
rename to app/models/concerns/tableless.rb
index 270e1bb1081..236497e6e47 100644
--- a/app/models/tableless_model.rb
+++ b/app/models/concerns/tableless.rb
@@ -26,9 +26,8 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class TablelessModel
- include ActiveModel::Model
- include ActiveModel::Attributes
+module Tableless
+ extend ActiveSupport::Concern
def persisted?
false
@@ -37,4 +36,14 @@ class TablelessModel
def readonly?
true
end
+
+ class_methods do
+ def load_schema!
+ # noop
+ end
+
+ def columns_hash
+ @columns_hash ||= {}
+ end
+ end
end
diff --git a/app/models/day.rb b/app/models/day.rb
index 035ee438baf..a182b8836db 100644
--- a/app/models/day.rb
+++ b/app/models/day.rb
@@ -26,45 +26,62 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Day < TablelessModel
- attribute :id, :integer, default: nil
- attribute :date, :date, default: nil
- attribute :day_of_week, :integer, default: nil
- attribute :working, :boolean, default: true
+class Day < ApplicationRecord
+ include Tableless
- delegate :name, to: :week_day, allow_nil: true
+ has_many :non_working_days,
+ inverse_of: false,
+ class_name: "NonWorkingDay",
+ foreign_key: :date,
+ primary_key: :date,
+ dependent: nil
- def non_working_days
- @non_working_days ||= NonWorkingDay.where(date: date)
- end
+ define_attribute :date, ActiveRecord::Type.lookup(:date), default: nil
+ define_attribute :day_of_week, ActiveRecord::Type.lookup(:integer), default: nil
+ define_attribute :working, ActiveRecord::Type.lookup(:boolean), default: "t"
- class << self
- def for_this_month
- today = Time.zone.today
- from = today.at_beginning_of_month
- to = today.next_month.at_end_of_month
+ delegate :name, to: :week_day, allow_nil: true
- from_range(from: from, to: to)
- end
+ scope :working, -> { where(working: true) }
- def from_range(from:, to:)
- range = from.to_date..to.to_date
- non_working_days = NonWorkingDay.where(date: range).pluck(:date)
+ 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")
+ end
- 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.from_range(from:, to:)
+ from(Arel.sql(from_sql(from:, to:)))
+ end
+
+ 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
- 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
+ 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
end
def week_day
diff --git a/app/models/queries/days/day_query.rb b/app/models/queries/days/day_query.rb
index 74b350c4025..38db47af417 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.for_this_month
+ Day.default_scope
end
##
@@ -48,4 +48,8 @@ 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 709cc2142c0..54ea13a022b 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_range(from:, to:)
+ model.from_sql(from:, to:)
end
def type_strategy
@@ -57,6 +57,6 @@ class Queries::Days::Filters::DatesIntervalFilter < Queries::Days::Filters::DayF
end
def connection
- nil
+ ActiveRecord::Base::connection
end
end