require 'active_mocker/active_mock' Object.send(:remove_const, "<%= class_name + @mock_append_name %>") if Object.const_defined?("<%= class_name + @mock_append_name %>") class <%= class_name + @mock_append_name %> < ActiveMock::Base <% constants.each do |constant| %> <%= constant.first %> = <%= constant.last.inspect %> <% end -%> class << self def attributes @attributes ||= HashWithIndifferentAccess.new(<%= attributes_with_defaults %>) end def types @types ||= <%= types_hash %> end def associations @associations ||= <%= associations %> end def model_instance_methods @model_instance_methods ||= <%= instance_method_not_implemented %> end def model_class_methods @model_class_methods ||= <%= class_method_not_implemented %> end def mocked_class '<%= class_name %>' end def column_names attribute_names end 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, <%= 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? 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 %>, nil) 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 %> @associations['<%= meth.name %>'] end def <%= meth.name %>=(val) @associations['<%= meth.name %>'] = 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 %>').create(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 %>] ||= ActiveMock::HasMany.new([],'<%= meth.foreign_key %>', @attributes['id'], classes('<%= meth.class_name %>')) end def <%= meth.name %>=(val) @associations[:<%= meth.name %>] ||= ActiveMock::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 %>] ||= ActiveMock::HasAndBelongsToMany.new end def <%= meth.name %>=(val) @associations[:<%= meth.name %>] = ActiveMock::HasAndBelongsToMany.new(val) end <% end -%> ################################## # Model Methods getter/setters # ################################## <% instance_methods.each do |method| %> def <%= method.name %>(<%= method.arguments %>) block = model_instance_methods['<%= method.name %>'] self.class.is_implemented(block, '#<%= method.name %>') block.call(*[<%= method.arguments.passable %>]) end <% end -%> <% class_methods.each do |method| %> def self.<%= method.name %>(<%= method.arguments %>) block = model_class_methods['<%= method.name %>'] is_implemented(block, '::<%= method.name %>') block.call(*[<%= method.arguments.passable %>]) end <% end -%> private def self.reload load __FILE__ end end