Module: Guise::Syntax
- Defined in:
- lib/guise/syntax.rb
Instance Method Summary collapse
-
#guise_for(source_class_name, options = {}) ⇒ Object
Configures the other end of the association defined by #has_guises.
-
#guise_of(source_class_name) ⇒ Object
Specifies that the model is a subclass of a model configured with #has_guises specified by
class_name
. -
#has_guises(*guises, options) ⇒ Object
Setup the model's
guises
association. -
#scoped_guise_for(association_class_name) ⇒ Object
Specifies that the model is a subclass of a model configured with #guise_for specified by
class_name
.
Instance Method Details
#guise_for(source_class_name, options = {}) ⇒ Object
Configures the other end of the association defined by #has_guises. Defines equivalent scopes defined on the model configured with #has_guises.
Given the following configuring of has_guises
:
class User < ActiveRecord::Base
has_guises :DeskWorker, :MailForwarder, association: :roles, attribute: :title
end
The following call to guise_for
:
class Role < ActiveRecord::Base
guise_for :User
end
Is equivalent to:
class Role < ActiveRecord::Base
belongs_to :user
validates :title, presence: true, uniqueness: { scope: :user_id }, inclusion: { in: %w( DeskWorker MailForwarder ) }
scope :desk_workers, -> { where(title: "DeskWorker") }
scope :mail_forwarder, -> { where(title: "MailForwarder") }
end
131 132 133 |
# File 'lib/guise/syntax.rb', line 131 def guise_for(source_class_name, = {}) Guise.register_association(self, source_class_name, ) end |
#guise_of(source_class_name) ⇒ Object
Specifies that the model is a subclass of a model configured with
#has_guises specified by class_name
.
Configures the caller with the correct default_scope
. Given the
following definition with has_guises
:
class User < ActiveRecord::Base
has_guises :DeskWorker, :MailForwarder, association: :roles, attribute: :title
end
The following call to guise_of
:
class DeskWorker < User guise_of :User end
Is equivalent to:
```ruby
class DeskWorker < User
default_scope -> { desk_workers }
after_initialize do
self.guises.build(title: 'DeskWorker')
end
after_create do
self.guises.create(title: 'DeskWorker')
end
end
84 85 86 87 88 89 |
# File 'lib/guise/syntax.rb', line 84 def guise_of(source_class_name) = Guise.registry[source_class_name] default_scope GuiseOfScope.new(name, ) after_initialize SourceCallback.new(name, .attribute) end |
#has_guises(*guises, options) ⇒ Object
Setup the model's guises
association. Given the following setup:
class User < ActiveRecord::Base
has_guises :DeskWorker, :MailForwarder, association: :roles, attribute: :value
end
The following is configured:
has_many
association named according to the:association
option.User.desk_workers
andUser.mail_forwarders
model scopes.User#has_guise?
that checks if a user is a particular type.User#desk_worker?
,User#mail_forwarder?
that proxy toUser#has_guise?
.User#has_guises?
that checks if a user has records for all the types supplied. This is aliased toUser#has_roles?
.User#has_any_guises?
that checks if a user has records for any of the types supplied. This is aliased toUser#has_any_roles?
.- If the association name is not
:guises
:- Aliases the association methods to equivalent methods for
guises
(i.e.guises=
toroles=
). - Aliases the introspection methods (i.e.
has_guise?
tohas_role?
andhas_any_guises?
tohas_any_roles?
- Aliases the association methods to equivalent methods for
41 42 43 44 45 |
# File 'lib/guise/syntax.rb', line 41 def has_guises(*guises) include Introspection Guise.register_source(self, *guises) end |
#scoped_guise_for(association_class_name) ⇒ Object
Specifies that the model is a subclass of a model configured with
#guise_for specified by class_name
. The name of the calling class must
be <value|parent_class_name>
.
Given the following configuration with guise_for
:
class Role < ActiveRecord::Base
guise_for :User
end
The following call to scoped_guise_for
:
class DeskWorkerRole < Role
scoped_guise_for :Role
end
Is equivalent to:
class DeskWorkerRole < Role
default_scope -> { desk_workers }
after_initialize do
self.title = "DeskWorker"
end
end
169 170 171 172 173 174 175 |
# File 'lib/guise/syntax.rb', line 169 def scoped_guise_for(association_class_name) = Guise.registry[association_class_name] value = name.chomp(.association_class.name) default_scope .scope(value, :guise_for) after_initialize AssociationCallback.new(value, .attribute) end |