Rails simulating Constraints and sub-Constraints

我与影子孤独终老i 提交于 2019-12-13 17:55:33

问题


I am building a scheduling platform with tasks for a current project

Let's say there are some Global Constraints (money, people, etc.) for the project, and that each tasks also has individual Constraints.

The big deal is that I have to match each Global Constraint to every Constraint of the tasks of a same project

Now the real big deal, is that in addition to having a nature (financial -> Float, people -> Integer) the constraints can also have a type (ex : costs of wages, costs of raw materials, etc.)

What would be the rails way to do it ?

I thought of something like that, but it looks(?) a bit too repetitive :

class Constraint
    field :type, type: String
    belongs_to :task
    belongs_to :global_constraint
end

class Constraint::Financial < Constraint
    field :cost, type: Float
end

class Constraint::People < Constraint
    field :number, type: Integer
end
...

Now the problem is that I would need a similar structure for the GlobalConstraint

class GlobalConstraint
    field :type
    belongs_to :project
    has_many :constraints, polymorphic: true
end

class GlobalConstraint::Financial < Constraint
    field :cost, type: Integer
end

as for the matching algorithm, I would like to do something like this

(inside the Constraint class)

def find_global_constraint
        self.global_constraint = self.task.project.global_constraints.find_by(:type => self.type)
end

(inside GlobalConstraint::xxx class) :

def check_integrity
    "Requires custom implementation !"
end

Examples :

GlobalConstraint::Financial
def check_integrity
    sum = Float.new
    self.sonstraints.each do |constraint|
        sum += constraint.cost
    end
    sum < cost
end

GlobalConstraint::People
def check_integrity
    sum = Integer.new
    self.sonstraints.each do |constraint|
        sum += constraint.cost
    end
    sum < number
end

来源:https://stackoverflow.com/questions/25522767/rails-simulating-constraints-and-sub-constraints

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!