require 'active_mocker/mock' class <%= class_name + @mock_append_name %> < <%= parent_class %> created_with('<%= ActiveMocker::VERSION %>') <% constants.each do |constant| -%> <%= constant.first %> = <%= constant.last.inspect %> <% end -%> <% modules[:included].each do |constant| -%> prepend <%= constant %> <% end -%> <% modules[:extended].each do |constant| -%> extend <%= constant %> <% end -%> class << self def attributes @attributes ||= HashWithIndifferentAccess.new(<%= attributes_with_defaults %>).merge(super) end def types @types ||= ActiveMocker::Mock::HashProcess.new(<%= types_hash %>, method(:build_type)).merge(super) end def associations @associations ||= <%= associations %>.merge(super) end def associations_by_class @associations_by_class ||= <%= associations_by_class %>.merge(super) end def mocked_class <%= class_name.inspect %> end private :mocked_class def attribute_names @attribute_names ||= <%= attribute_names %> | super end def primary_key <%= primary_key.name.inspect %> end def abstract_class? <%= abstract_class.inspect %> end def table_name <%= table_name.inspect %> || super end end ################################## # Attributes getter/setters # ################################## <% attributes.each do |meth| %> def <%= meth.name %> read_attribute(:<%= meth.name %>) end def <%= meth.name %>=(val) <% association = belongs_to_foreign_key(meth.name) -%> write_attribute(:<%= meth.name %>, val) <% if association -%> association = classes('<%= association.class_name %>').try(:find_by, id: <%= meth.name %>) write_association(:<%= association.name %>,association) unless association.nil? <% end -%> end <% end %> ################################## # Associations # ################################## <%= '# belongs_to' unless belongs_to.empty? -%> <% belongs_to.each do |meth| %> def <%= meth.name %> read_association(:<%= meth.name %>) end def <%= meth.name %>=(val) write_association(:<%= meth.name %>, val) ActiveMocker::Mock::BelongsTo.new(val, child_self: self, foreign_key: :<%= meth.foreign_key %>, foreign_id: val.try(:id)).item end def build_<%= meth.name %>(attributes={}, &block) <% association = relation_find(:name, meth.name).first -%> <% if association -%> association = classes('<%= association.class_name %>').try(:new, attributes, &block) write_association(:<%= meth.name %>, association) unless association.nil? <% end -%> end def create_<%= meth.name %>(attributes={}, &block) <% association = relation_find(:name, meth.name).first -%> <% if association -%> association = classes('<%= association.class_name %>').try(:create,attributes, &block) write_association(:<%= meth.name %>, association) unless association.nil? <% end -%> end alias_method :create_<%= meth.name %>!, :create_<%= meth.name %> <% end -%> <%= '# has_one' unless has_one.empty? -%> <% has_one.each do |meth| %> def <%= meth.name %> read_association(:<%= meth.name %>) end def <%= meth.name %>=(val) write_association(:<%= meth.name %>, val) ActiveMocker::Mock::HasOne.new(val, child_self: self, foreign_key: '<%= meth.foreign_key %>', foreign_id: self.id).item end def build_<%= meth.name %>(attributes={}, &block) <% association = relation_find(:name, meth.name).first -%> <% if association -%> write_association(:<%= meth.name %>, classes('<%= association.class_name %>').new(attributes, &block)) if classes('<%= association.class_name %>') <% end -%> end def create_<%= meth.name %>(attributes={}, &block) <% association = relation_find(:name, meth.name).first -%> <% if association -%> write_association(:<%= meth.name %>, classes('<%= association.class_name %>').new(attributes, &block)) if classes('<%= association.class_name %>') <% end -%> end alias_method :create_<%= meth.name %>!, :create_<%= meth.name %> <% end -%> <%= '# has_many' unless has_many.empty? -%> <% has_many.each do |meth| %> def <%= meth.name %> read_association(:<%= meth.name %>, -> { ActiveMocker::Mock::HasMany.new([],foreign_key: '<%= meth.foreign_key %>', foreign_id: self.id, relation_class: classes('<%= meth.class_name %>'), source: '<%= meth.source %>') }) end def <%= meth.name %>=(val) write_association(:<%= meth.name %>, ActiveMocker::Mock::HasMany.new(val, foreign_key: '<%= meth.foreign_key %>', foreign_id: self.id, relation_class: classes('<%= meth.class_name %>'), source: '<%= meth.source %>')) end <% end -%> <%= '# has_and_belongs_to_many' unless has_and_belongs_to_many.empty? -%> <% has_and_belongs_to_many.each do |meth| %> def <%= meth.name %> read_association(:<%= meth.name %>, ->{ ActiveMocker::Mock::HasAndBelongsToMany.new([]) }) end def <%= meth.name %>=(val) write_association(:<%= meth.name %>, ActiveMocker::Mock::HasAndBelongsToMany.new(val)) end <% end -%> module Scopes include <%= parent_class %>::Scopes <% scope_methods.each do |method| -%> def <%= method.name %><%= "(#{method.arguments})" unless method.arguments.empty? %> ActiveMocker::LoadedMocks.find('<%= class_name %>').send(:call_mock_method, '<%= method.name %>', <%= method.arguments.passable %>) end <% end -%> end extend Scopes class ScopeRelation < ActiveMocker::Mock::Association include <%= class_name + @mock_append_name %>::Scopes end private def self.new_relation(collection) <%= class_name + @mock_append_name %>::ScopeRelation.new(collection) end public ################################## # Model Methods # ################################## <% instance_methods.each do |method| %> def <%= method.name %><%= "(#{method.arguments})" unless method.arguments.empty? %> call_mock_method :<%= method.name %><%= ', ' unless method.arguments.empty? %><%= method.arguments.passable %> end <% end -%> <% class_methods.each do |method| %> def self.<%= method.name %><%= "(#{method.arguments})" unless method.arguments.empty? %> call_mock_method :<%= method.name %><%= ', ' unless method.arguments.empty? %><%= method.arguments.passable %> end <% end -%> end