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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
fix tableless models
Klaus Zanders478519db1427
diff --git a/app/models/action.rb b/app/models/action.rbindex 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 Tablelessinclude Scopes::Scopedscopes :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: falseenddiff --git a/app/models/capability.rb b/app/models/capability.rbindex 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 Tablelessinclude Scopes::Scopedscopes :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: nilenddiff --git a/app/models/tableless_model.rb b/app/models/concerns/tableless.rbsimilarity index 87%rename from app/models/tableless_model.rbrename to app/models/concerns/tableless.rbindex 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::Concerndef persisted?false@@ -37,4 +36,14 @@ class TablelessModeldef readonly?trueend++ class_methods do+ def load_schema!+ # noop+ end++ def columns_hash+ @columns_hash ||= {}+ end+ endenddiff --git a/app/models/day.rb b/app/models/day.rbindex 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).lastenddef week_daydiff --git a/app/models/queries/days/day_query.rb b/app/models/queries/days/day_query.rbindex 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::DayQueryenddef default_scope- Day.for_this_month+ Day.default_scopeend##@@ -48,4 +48,8 @@ class Queries::Days::DayQueryscope = scope.from(from_clause_filter.from) if from_clause_filterscopeend++ def results+ super.reorder(date: :asc)+ endenddiff --git a/app/models/queries/days/filters/dates_interval_filter.rb b/app/models/queries/days/filters/dates_interval_filter.rbindex 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::DayFto = from.next_month.at_end_of_monthend- model.from_range(from:, to:)+ model.from_sql(from:, to:)enddef type_strategy@@ -57,6 +57,6 @@ class Queries::Days::Filters::DatesIntervalFilter < Queries::Days::Filters::DayFenddef connection- nil+ ActiveRecord::Base::connectionendend