Module: ActsAsActsAs::Macros

Included in:
Requirements::Verifier
Defined in:
lib/acts_as_acts_as/macros.rb

Overview

Methods available within examples

Instance Method Summary collapse

Instance Method Details

#drop_temp_model(tmodel) ⇒ Object



85
86
87
88
# File 'lib/acts_as_acts_as/macros.rb', line 85

def drop_temp_model(tmodel)
  ActiveRecord::Base.connection.drop_table(tmodel.table_name)
  ActiveRecord::Base.connection.drop_table(tmodel.versioned_table_name) if tmodel.respond_to?(:versioned_table_name)
end

#tableless_model(options) ⇒ Object

ActsAsActsAs::Macros#tableless_model a factory method for an ActiveRecord model using the ‘active_record_tableless’ plugin.

Parameters

* <tt>:class_name</tt> - if you want the class to be assigned a constant,
                         specify it as a string here, note that if you want
                         the constant to be namespaced you should do the
                         nested const_sets yourself and leave this option
                         blank
* <tt>:superclass</tt> - superclass for the new model, defaults to
                         ActiveRecord::Base, as you might expect
* <tt>:columns</tt>    - collection of tuples as expected by the
                         <tt>active_record_tableless</tt> plugin to be
                         added as columns to the  created model
* <tt>:methods</tt>    - collection of symbols to define by attr_accessor
                         in the created model


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/acts_as_acts_as/macros.rb', line 20

def tableless_model(options)
  options    = options.dup
  class_name = options.delete(:class_name) || nil
  superclass = options.delete(:superclass) || ActiveRecord::Base
  columns    = options.delete(:columns)    || []
  methods    = options.delete(:methods)    || []

  newclass = Class.new(superclass)

  newclass.class_eval do
    tableless :columns => columns
    methods.each do |m|
      attr_accessor m
    end
  end

  Object.const_set(class_name, newclass) if class_name
  return newclass
end

#temp_model(model_opts) {|newclass| ... } ⇒ Object

A model with a table that exists only for the duration of the block, or if no block is passed, until drop_temp_model is called

Yields:

  • (newclass)


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
# File 'lib/acts_as_acts_as/macros.rb', line 42

def temp_model(model_opts, &block)
  t = Time.now
  uid = t.to_i.to_s + t.usec.to_s
  table_name = "temp_table_#{uid}"
  ActiveRecord::Base.connection.create_table(table_name) do |t|
    model_opts[:columns].each do |args|
      (name, type, options) = args
      options = {} if options.nil?
      t.send(type, name, options)
    end
  end

  newclass = Class.new(ActiveRecord::Base)
  Object.const_set(table_name.camelize, newclass)
  newclass.set_table_name table_name
  newclass.reset_column_information

  if model_opts[:methods]
    newclass.class_eval do
      model_opts[:methods].each do |m|
        attr_accessor m
      end
    end
  end

  return newclass unless block_given?

  yield newclass

  drop_temp_model(newclass)
end

#temp_versioned_model(model_opts) {|model| ... } ⇒ Object

Yields:

  • (model)


74
75
76
77
78
79
80
81
82
# File 'lib/acts_as_acts_as/macros.rb', line 74

def temp_versioned_model(model_opts, &block)
  model = temp_model(model_opts)
  silence_stream(STDERR) { model.acts_as_ris_versioned(model_opts[:aav_options] || {}) }
  model.update_versioned_table

  return model unless block_given?
  yield model
  drop_temp_model(model)
end