Module: ControlledVersioning::ActsAsVersionable::ClassMethods

Defined in:
lib/controlled_versioning.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_versionable(options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/controlled_versioning.rb', line 32

def acts_as_versionable(options = {})

  send :include, InstanceMethods

  def define_callbacks(*types)
    types.each { |type| define_callback(type) }
  end

  def define_callback(type)
    define_singleton_method("after_#{type}_a_version") do |*args|
      options = args.extract_options!
      options.merge!(only: :general) if options[:only].blank?
      register_callback_methods(type, args, options)
    end
  end

  def register_callback_methods(type, args, options)
    restriction = options[:only].to_s
    define_singleton_method("#{restriction}_#{type}_callbacks") do
      args
    end
  end

  define_callbacks :creating, :accepting, :declining

  def set_versionable_attribute_names(nonversionables)
    nonversionables = [] unless nonversionables.present?
    ArrayConverter.to_s!(nonversionables)
    nonversionables += ["id", "updated_at", "created_at"]
    attribute_names - nonversionables
  end

  def nested_associations
    nested_attributes_options.keys
  end

  def has_nested_associations?
    nested_associations.present?
  end

  attr_accessor :user, :notes

  cattr_accessor :nested_within
  self.nested_within = options[:nested_within]

  def is_a_nested_association?
    nested_within.present?
  end

  if is_a_nested_association?
    has_many :version_children,
             class_name: 'ControlledVersioning::VersionChild',
             as: :versionable
  else
    has_many :versions,
             lambda { order("created_at ASC")},
             class_name: 'ControlledVersioning::Version',
             as: :versionable
  end

  cattr_accessor :versionable_attribute_names
  self.versionable_attribute_names = options[:versionable_attributes] ||
                                     set_versionable_attribute_names(
                                     options[:nonversionable_attributes])
  
  def new_with_version(attributes)
    resource = self.new(attributes)
    return resource.errors if resource.invalid?
    resource.build_initial_version
    resource
  end

  def create_with_version(attributes)
    resource = new_with_version(attributes)
    resource.save
    resource
  end
end

#create_with_version(attributes) ⇒ Object



104
105
106
107
108
# File 'lib/controlled_versioning.rb', line 104

def create_with_version(attributes)
  resource = new_with_version(attributes)
  resource.save
  resource
end

#define_callback(type) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/controlled_versioning.rb', line 40

def define_callback(type)
  define_singleton_method("after_#{type}_a_version") do |*args|
    options = args.extract_options!
    options.merge!(only: :general) if options[:only].blank?
    register_callback_methods(type, args, options)
  end
end

#define_callbacks(*types) ⇒ Object



36
37
38
# File 'lib/controlled_versioning.rb', line 36

def define_callbacks(*types)
  types.each { |type| define_callback(type) }
end

#has_nested_associations?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/controlled_versioning.rb', line 68

def has_nested_associations?
  nested_associations.present?
end

#is_a_nested_association?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/controlled_versioning.rb', line 77

def is_a_nested_association?
  nested_within.present?
end

#nested_associationsObject



64
65
66
# File 'lib/controlled_versioning.rb', line 64

def nested_associations
  nested_attributes_options.keys
end

#new_with_version(attributes) ⇒ Object



97
98
99
100
101
102
# File 'lib/controlled_versioning.rb', line 97

def new_with_version(attributes)
  resource = self.new(attributes)
  return resource.errors if resource.invalid?
  resource.build_initial_version
  resource
end

#register_callback_methods(type, args, options) ⇒ Object



48
49
50
51
52
53
# File 'lib/controlled_versioning.rb', line 48

def register_callback_methods(type, args, options)
  restriction = options[:only].to_s
  define_singleton_method("#{restriction}_#{type}_callbacks") do
    args
  end
end

#set_versionable_attribute_names(nonversionables) ⇒ Object



57
58
59
60
61
62
# File 'lib/controlled_versioning.rb', line 57

def set_versionable_attribute_names(nonversionables)
  nonversionables = [] unless nonversionables.present?
  ArrayConverter.to_s!(nonversionables)
  nonversionables += ["id", "updated_at", "created_at"]
  attribute_names - nonversionables
end