Many rails Models with the same flag. What is the best practice?

In one of the projects of my rails there are many models with the same flag: approved. I don’t like to fly an “approved” flag for so many models, and I'm looking for a DRY solution. I found some plugin like flag_shih_tzu or can_flag, but I think they only work with the model.

Do you know that any plugin allows you to specify multiple models at once? I think that I am a good solution (without a plugin) should use polymorphic associations, do you agree?

Thanks a lot, Alessandro

+3
source share
3 answers

, , , , mixin . , lib/approved.rb :

module Approved

    # Any approval functions/constants that don't belong in a model go here...

    module Mixin
        def self.included(klass)
            klass.class_eval do
                # Class-levell model macros can be run here
                named_scope :approved,   {:conditions => {:approved => true}}
                named_scope :unapproved, {:conditions => {:approved => false}}
            end
        end

        def approved?
            return (self.approved == true)
        end

        # Other shared model functions go here...
    end
end

mixin , :

class Approvable < ActiveRecord::Base
    include Approved::Mixin

    # etc.
end

, !

+4

, 10 , . transitions , approved state.

, , :

class Comment < A:RB
  include ApprovalWorkflow
end

, :

# /app/workflows/approval_workflow.rb
module ApprovalWorkflow
  def self.included(klass)
    klass.class_eval do
      state_machine do
        .. workflow junk goes here .. 
      end
    end
  end
end

, , , , , ( , , ), , . !

, , , , approve!(user) .

, .

+1

, . , is-a .

, Approval " " .

0

Source: https://habr.com/ru/post/1795793/


All Articles