Hi, I'm trying to create a simple plugin that should set the value for the 
column for the Sequel::Model. I noticed that when I'm trying to assign 
records using add_ method all the hooks are omitted for the join model. 
It's intentional? There is any way to catch any hook for that record?

user = User[args[:id]]
user.add_group(UsersGroups::General[group_id])

class User < Sequel::Model
...
class << self
private

def groups_associations_options
  {
    left_key: :user_id,
    right_key: :users_group_id,
    join_table: :users_groups_users,
  }
  end
end

many_to_many(
  :general_groups,
  class: 'UsersGroups::General',
  **groups_associations_options
)
many_to_many(
  :groups,
  class: :UsersGroup,
  **groups_associations_options
)
...
end

module UsersGroups
  class General < UsersGroup
    many_to_many(
      :users,
      class: :User,
      join_table: :users_groups_users,
      left_key: :users_group_id,
      right_key: :user_id
    )
  end
end

class UsersGroupUser < Sequel::Model(:users_groups_users)
  many_to_one :organization
  many_to_one :user
  many_to_one :users_group
end

module Sequel
  module Plugins
    module OrganizationIdentity
      def self.configure(model, opts=OPTS)
        model.instance_exec do
          @ignored_models = opts[:ignored_models]||[]
        end
      end
      module ClassMethods
        Plugins.inherited_instance_variables(self, :@ignored_models => nil)

        attr_reader :ignored_models
      end
      module InstanceMethods
        def before_create
          #want to catch UsersGroupUser instance over here
          set_organization_identity if new?
          super
        end

        private

        def set_organization_identity
          unless model.ignored_models.include?(model.name)
            field = 'organization_id'
            meth = :"#{field}="
            set_column_value(meth, get_column_value(field).presence || 
Current.organization_id) if respond_to?(field) && respond_to?(meth)
          end
        end
      end
    end
  end
end

-- 






 <https://www.start-up.house>
www.start-up.house 
<https://www.start-up.house>



 
<https://linkedin.com/company/start-up-development-house>
Follow us on 
Linkedin <https://linkedin.com/company/start-up-development-house>



 
<https://facebook.com/startup.development.house>
Follow us on facebook 
<https://facebook.com/startup.development.house>







 


Startup 
Development House Sp. z o.o.,Al. Niepodległości 124/23 02-577 Warsaw, 
Poland, registered in the Register of Entrepreneurs (maintained at the 
District Court for the Capital City of Warsaw, XIII Business Department) 
under the National Court Register no. 0000624654. Tax Identification Number 
(NIP): PL5213739631, National Economy Register Number (REGON): 364787848, 
share capital: 30 000 PLN

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/5ae4c460-ea17-4ec0-bfbb-d0435a565acfn%40googlegroups.com.

Reply via email to