Skip to main content
adicionou 34 caracteres ao conteúdo
Fonte Link
Bmucelini
  • 345
  • 1
  • 13
# app/models/concerns/person_helper.rb
module PersonHelper
    extend ActiveSupport::Concern
    included do
        validates :name, presence: true
    end
end


# app/models/concerns/is_a_person.rb
module IsAPerson
    extend ActiveSupport::Concern
    included do
        include PersonHelper
        belongs_to :person, autosave: true
        delegate_associations to: :person
        delegate_attributes to: :person
        scopedef :self.find_by_id, ->(idvalue) 
 {           find_by_person_id(idvalue) 
 }       end
        def person
            super || build_person
        end
    end    
end
# app/models/concerns/person_helper.rb
module PersonHelper
    extend ActiveSupport::Concern
    included do
        validates :name, presence: true
    end
end


# app/models/concerns/is_a_person.rb
module IsAPerson
    extend ActiveSupport::Concern
    included do
        include PersonHelper
        belongs_to :person, autosave: true
        delegate_associations to: :person
        delegate_attributes to: :person
        scope :find_by_id, ->(id) { find_by_person_id(id) }
        def person
            super || build_person
        end
    end    
end
# app/models/concerns/person_helper.rb
module PersonHelper
    extend ActiveSupport::Concern
    included do
        validates :name, presence: true
    end
end


# app/models/concerns/is_a_person.rb
module IsAPerson
    extend ActiveSupport::Concern
    included do
        include PersonHelper
        belongs_to :person, autosave: true
        delegate_associations to: :person
        delegate_attributes to: :person
        def self.find_by_id(value) 
            find_by_person_id(value) 
        end
        def person
            super || build_person
        end
    end    
end
adicionou 16 caracteres ao conteúdo
Fonte Link
Bmucelini
  • 345
  • 1
  • 13
# app/models/concerns/person_helper.rb
module PersonHelper
    extend ActiveSupport::Concern
    included do
        validates :name, presence: true
    end
end


# app/models/concerns/is_a_person.rb
module IsAPerson
    extend ActiveSupport::Concern
    included do
        include PersonHelper
        belongs_to :person, autosave: true
        delegate_associations to: :person
        delegate_attributes to: :person
        scope :find_by_id, ->(id) { find_by_person_id(id) }
        def person
            super || build_person
        end
    end    
end
# app/models/concerns/person_helper.rb
module PersonHelper
    extend ActiveSupport::Concern
    included do
        validates :name, presence: true
    end
end


# app/models/concerns/is_a_person.rb
module IsAPerson
    extend ActiveSupport::Concern
    included do
        include PersonHelper
        belongs_to :person
        delegate_associations to: :person
        delegate_attributes to: :person
        scope :find_by_id, ->(id) { find_by_person_id(id) }
        def person
            super || build_person
        end
    end    
end
# app/models/concerns/person_helper.rb
module PersonHelper
    extend ActiveSupport::Concern
    included do
        validates :name, presence: true
    end
end


# app/models/concerns/is_a_person.rb
module IsAPerson
    extend ActiveSupport::Concern
    included do
        include PersonHelper
        belongs_to :person, autosave: true
        delegate_associations to: :person
        delegate_attributes to: :person
        scope :find_by_id, ->(id) { find_by_person_id(id) }
        def person
            super || build_person
        end
    end    
end
Fonte Link
Bmucelini
  • 345
  • 1
  • 13

Bom, vou postar aqui mesmo qual foi a solução que encontrei e atende todas as minhas necessidades.

Primeiramente criei a gem delegate_associations e adicionei em meu projeto.

O Passo seguinte foi ajustar minhas migrações:

class CreatePeople < ActiveRecord::Migration
  def change
    create_table :people do |t|
      t.string :name
      t.string :cpf_cnpj    
      t.timestamps
    end
  end
end

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users, id: false do |t|
      t.primary_key :person_id
      t.string :login
      t.string :password
      t.timestamps
    end
  end
end

class CreateSellers < ActiveRecord::Migration
  def change
    create_table :sellers, id: false do |t|
      t.primary_key :person_id
      t.decimal :comission, default: 0.0
      t.timestamps
    end
  end
end

Percebam que eu removi a coluna id de User e Seller e adicionei como chave primária a coluna person_id. Fiz isso para que quando eu associar por exemplo um Seller para uma venda, sempre vou ter o id da Person que é o mesmo do Seller.

Depois disso criei 2 Modules:

# app/models/concerns/person_helper.rb
module PersonHelper
    extend ActiveSupport::Concern
    included do
        validates :name, presence: true
    end
end


# app/models/concerns/is_a_person.rb
module IsAPerson
    extend ActiveSupport::Concern
    included do
        include PersonHelper
        belongs_to :person
        delegate_associations to: :person
        delegate_attributes to: :person
        scope :find_by_id, ->(id) { find_by_person_id(id) }
        def person
            super || build_person
        end
    end    
end

Meus modelos ficaram da seguinte forma:

# app/models/person.rb
class Person < ActiveRecord::Base
    include PersonHelper

    has_one :user
    accepts_nested_attributes_for :user
    has_one :seller
    accepts_nested_attributes_for :seller
end

# app/models/user.rb
class User < ActiveRecord::Base
    include IsAPerson
    validates :login, :password, presence: true
end

# app/models/seller.rb
class Seller < ActiveRecord::Base
    include IsAPerson
end

Agora todas as validações e metodos que uma pessoa tem eu não vou colocar diretamente no model de Person e sim no module PersonHelper para que meu User e meu Seller possam ter também as mesmas coisas.

Com isso posso ter por exemplo essas possibilidades:

> user = User.new(name: 'Nome da pessoa', login: 'admin', password: '123456')
> user.save # true
> user.person.persisted? # true
> user.seller #nil
> user.build_seller # Irá instanciar um Seller e associar com a Person
> user.seller.comission # 0.0

> user.name = 'Joe'
> user.person.name # Joe

Percebam que fica como se o User tivesse o atributo name, e toda a vez que eu seto um novo name para o user ele seta automaticamente na person. Isso acontece pelo uso da gem que eu fiz onde utilizo os artifícios do delegate.

Talvez essa não seja a opção mais adequada, porém supriu todas as minhas necessidades e funciona perfeitamente.