require 'active_mocker/mock' class <%= class_name + @mock_append_name %> < ActiveMocker::Mock::Base <% 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 %>) end def types @types ||= ActiveMocker::Mock::HashProcess.new(<%= types_hash %>, method(:build_type)) end def associations @associations ||= <%= associations %> end def mocked_class '<%= class_name %>' end private :mocked_class def attribute_names @attribute_names ||= <%= attribute_names %> end def primary_key <%= primary_key.name.inspect %> 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 %> @associations[:<%= meth.name %>] end def <%= meth.name %>=(val) @associations[:<%= meth.name %>] = val write_attribute(:<%= meth.foreign_key %>, val.id) if val.respond_to?(:persisted?) && val.persisted? if ActiveMocker::Mock.config.experimental val.<%= class_name.tableize %> << self if val.respond_to?(:<%= class_name.tableize %>=) val.<%= class_name.tableize.singularize %> = self if val.respond_to?(:<%= class_name.tableize.singularize %>=) end val 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) @associations['<%= meth.name %>'] = val if ActiveMocker::Mock.config.experimental <%= meth.name %>.<%= class_name.tableize %> << self if val.respond_to?(:<%= class_name.tableize %>=) <%= meth.name %>.send(:write_association, :<%= class_name.tableize.singularize %>, self) if val.respond_to?(:<%= class_name.tableize.singularize %>=) end val 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 %> @associations[:<%= meth.name %>] ||= ActiveMocker::Mock::HasMany.new([],'<%= meth.foreign_key %>', @attributes['id'], classes('<%= meth.class_name %>')) end def <%= meth.name %>=(val) @associations[:<%= meth.name %>] ||= ActiveMocker::Mock::HasMany.new(val,'<%= meth.foreign_key %>', @attributes['id'], classes('<%= meth.class_name %>')) 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 %> @associations[:<%= meth.name %>] ||= ActiveMocker::Mock::HasAndBelongsToMany.new([]) end def <%= meth.name %>=(val) @associations[:<%= meth.name %>] = ActiveMocker::Mock::HasAndBelongsToMany.new(val) end <% end -%> module 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 -%> private def self.reload load __FILE__ end end