jnstq (owner)

Revisions

gist: 184841 Download_button fork
public
Public Clone URL: git://gist.github.com/184841.git
Embed All Files: show embed
action_duck.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require 'activerecord'
require 'active_record/base'
require 'active_record/validations'
 
module ActionDuck
  
  def self.included(klass)
    klass.extend ClassMethods
    klass.send(:include, InstanceMethods)
  end
  
  module ClassMethods
    
    def self_and_descendants_from_active_record
      [self]
    end
 
    def human_name
      self.name.humanize
    end
 
    def human_attribute_name(attribute_key_name, options = {})
      attribute_key_name.to_s.humanize
    end
    
  end
  
  module InstanceMethods
    attr_accessor :errors
      
    def initialize(h={})
      h ||= {}
      h.each { |k,v| send("#{k}=", v) }
      self.errors = ActiveRecord::Errors.new(self)
    end
    
    # These make an instance quack like an AR::Base
    def new_record?
      true
    end
 
    def id
      nil
    end
  end
  
end