Class: ActiveRecord::Base

Inherits:
Object show all
Extended by:
ClassMethods
Includes:
InstanceMethods
Defined in:
lib/active_record_base.rb,
lib/reactive_record/permissions.rb,
lib/reactive_record/active_record/base.rb,
lib/reactive_record/active_record/aggregations.rb,
lib/reactive_record/active_record/associations.rb,
lib/synchromesh/reactive_record/permission_patches.rb,
lib/reactive_record/active_record/public_columns_hash.rb

Overview

adds method to get the HyperMesh public column types for now we are just getting all models column types, but we should look through the public folder, and just get those models. this works because the public folder is currently required to be eaer loaded.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from InstanceMethods

#backing_record

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

_react_param_conversion, abstract_class=, abstract_class?, base_class, column_names, columns_hash, composed_of, enum, find, find_by, inheritance_column, inheritance_column=, model_name, primary_key, primary_key=

Methods included from InstanceMethods

#==, #attributes, #changed?, #destroy, #destroyed?, #dup, #errors, #id, #id=, #initialize, #itself, #load, #model_name, #new?, #primary_key, #revert, #save, #saving?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/active_record_base.rb', line 49

def method_missing(name, *args, &block)
  #return get_by_index(*args).first if name == "[]"
  return all.send(name, *args, &block) if [].respond_to?(name)
  if name =~ /\!$/
    return send(name.gsub(/\!$/,''), *args, &block).send(:reload_from_db) rescue nil
  end
  pre_synchromesh_method_missing(name, *args, &block)
end

Class Attribute Details

.reactive_record_association_keysObject (readonly)

Returns the value of attribute reactive_record_association_keys.



59
60
61
# File 'lib/reactive_record/permissions.rb', line 59

def reactive_record_association_keys
  @reactive_record_association_keys
end

Instance Attribute Details

#acting_userObject

Returns the value of attribute acting_user.



11
12
13
# File 'lib/reactive_record/permissions.rb', line 11

def acting_user
  @acting_user
end

Class Method Details

._synchromesh_scope_args_check(args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/active_record_base.rb', line 9

def _synchromesh_scope_args_check(args)
  opts = if args.count == 2 && args[1].is_a?(Hash)
           args[1].merge(server: args[0])
         elsif args[0].is_a? Hash
           args[0]
         else
           { server: args[0] }
         end
  return opts if opts.is_a?(Hash) && opts[:server].respond_to?(:call)
  raise 'must provide either a proc as the first arg or by the '\
        '`:server` option to scope and default_scope methods'
end

.belongs_to(attr_name, scope = nil, options = {}) ⇒ Object



75
76
77
78
79
80
# File 'lib/reactive_record/permissions.rb', line 75

def belongs_to(attr_name, scope = nil, options = {})
  define_method "#{attr_name}_is?".to_sym do |model|
    send(options[:foreign_key] || "#{attr_name}_id") == model.id
  end
  belongs_to_without_reactive_record_add_is_method(attr_name, scope, options)
end

.belongs_to_without_reactive_record_add_is_methodObject



73
# File 'lib/reactive_record/permissions.rb', line 73

alias belongs_to_without_reactive_record_add_is_method belongs_to

.do_not_synchronizeObject



25
26
27
# File 'lib/active_record_base.rb', line 25

def do_not_synchronize
  @do_not_synchronize = true
end

.do_not_synchronize?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/active_record_base.rb', line 29

def do_not_synchronize?
  @do_not_synchronize
end

.pre_synchromesh_default_scopeObject



23
# File 'lib/active_record_base.rb', line 23

alias pre_synchromesh_default_scope default_scope

.pre_synchromesh_scopeObject



22
# File 'lib/active_record_base.rb', line 22

alias pre_synchromesh_scope scope

.public_columns_hashObject



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/reactive_record/active_record/public_columns_hash.rb', line 7

def self.public_columns_hash
  return @public_columns_hash if @public_columns_hash
  Dir.glob(Rails.root.join('app/models/public/*.rb')).each do |file|
    require_dependency(file) rescue nil
  end
  @public_columns_hash = {}
  descendants.each do |model|
    @public_columns_hash[model.name] = model.columns_hash
  end
  @public_columns_hash
end

.reflect_on_aggregation(attribute) ⇒ Object



9
10
11
# File 'lib/reactive_record/active_record/aggregations.rb', line 9

def self.reflect_on_aggregation(attribute)
  reflect_on_all_aggregations.detect { |aggregation| aggregation.attribute == attribute }
end

.reflect_on_all_aggregationsObject



5
6
7
# File 'lib/reactive_record/active_record/aggregations.rb', line 5

def self.reflect_on_all_aggregations
  base_class.instance_eval { @aggregations ||= [] }
end

.reflect_on_all_associationsObject



5
6
7
# File 'lib/reactive_record/active_record/associations.rb', line 5

def self.reflect_on_all_associations
  base_class.instance_eval { @associations ||= superclass.instance_eval { (@associations && @associations.dup) || [] } }
end

.reflect_on_association(attribute) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/reactive_record/active_record/associations.rb', line 9

def self.reflect_on_association(attribute)
  if found = reflect_on_all_associations.detect { |association| association.attribute == attribute and association.owner_class == self }
    found
  elsif superclass == Base
    nil
  else
    superclass.reflect_on_association(attribute)
  end
end

Instance Method Details

#<=>(other) ⇒ Object



144
145
146
# File 'lib/active_record_base.rb', line 144

def <=>(other)
  id.to_i <=> other.id.to_i
end

#allObject



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/active_record_base.rb', line 80

def all
  ReactiveRecord::Base.default_scope[self] ||=
    if @_default_scopes
      root = ReactiveRecord::Collection
             .new(self, nil, nil, self, 'all')
             .extend(ReactiveRecord::UnscopedCollection)
      @_default_scopes.inject(root) do |scope, opts|
        scope.build_child_scope(ReactiveRecord::ScopeDescription.new(self, :all, opts))
      end
    end || unscoped
end

#all=(_collection) ⇒ Object



92
93
94
# File 'lib/active_record_base.rb', line 92

def all=(_collection)
  raise "NO LONGER IMPLEMENTED DOESNT PLAY WELL WITH SYNCHROMESH"
end

#all_changed?(*attributes) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
# File 'lib/reactive_record/permissions.rb', line 50

def all_changed?(*attributes)
  attributes.each do |key|
    return false unless self.send("#{key}_changed?")
  end
  true
end

#any_changed?(*attributes) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
# File 'lib/reactive_record/permissions.rb', line 43

def any_changed?(*attributes)
  attributes.each do |key|
    return true if self.send("#{key}_changed?")
  end
  false
end

#check_permission_with_acting_user(user, permission, *args) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/reactive_record/permissions.rb', line 84

def check_permission_with_acting_user(user, permission, *args)
  old = acting_user
  self.acting_user = user
  if self.send(permission, *args)
    self.acting_user = old
    self
  else
    raise ReactiveRecord::AccessViolation, "for #{permission}(#{args})"
  end
end

#create(*args, &block) ⇒ Object



58
59
60
# File 'lib/active_record_base.rb', line 58

def create(*args, &block)
  new(*args).save(&block)
end

#create_permitted?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/reactive_record/permissions.rb', line 13

def create_permitted?
  true
end

#default_scope(*args, &block) ⇒ Object



40
41
42
43
# File 'lib/active_record_base.rb', line 40

def default_scope(*args, &block)
  opts = _synchromesh_scope_args_check([*block, *args])
  pre_synchromesh_default_scope(opts[:server], &block)
end

#destroy_permitted?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/reactive_record/permissions.rb', line 21

def destroy_permitted?
  true
end

#do_not_synchronize?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/active_record_base.rb', line 107

def do_not_synchronize?
  self.class.do_not_synchronize?
end

#none_changed?(*attributes) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
# File 'lib/reactive_record/permissions.rb', line 36

def none_changed?(*attributes)
  attributes.each do |key|
    return false if self.send("#{key}_changed?")
  end
  true
end

#only_changed?(*attributes) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
# File 'lib/reactive_record/permissions.rb', line 29

def only_changed?(*attributes)
  (self.attributes.keys + self.class.reactive_record_association_keys).each do |key|
    return false if self.send("#{key}_changed?") and !attributes.include? key
  end
  true
end

#pre_synchromesh_method_missingObject



47
# File 'lib/active_record_base.rb', line 47

alias pre_synchromesh_method_missing method_missing

#scope(name, *args) ⇒ Object



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

def scope(name, *args, &block)
  opts = _synchromesh_scope_args_check(args)
  pre_synchromesh_scope(name, opts[:server], &block)
end

#synchromesh_after_changeObject



120
121
122
123
# File 'lib/active_record_base.rb', line 120

def synchromesh_after_change
  return if do_not_synchronize? || previous_changes.empty?
  HyperMesh.after_commit :change, self
end

#synchromesh_after_createObject



115
116
117
118
# File 'lib/active_record_base.rb', line 115

def synchromesh_after_create
  return if do_not_synchronize? || previous_changes.empty?
  HyperMesh.after_commit :create, self
end

#synchromesh_after_destroyObject



125
126
127
128
# File 'lib/active_record_base.rb', line 125

def synchromesh_after_destroy
  return if do_not_synchronize?
  HyperMesh.after_commit :destroy, self
end

#unscopedObject



96
97
98
99
100
101
# File 'lib/active_record_base.rb', line 96

def unscoped
  ReactiveRecord::Base.unscoped[self] ||=
    ReactiveRecord::Collection
    .new(self, nil, nil, self, 'unscoped')
    .extend(ReactiveRecord::UnscopedCollection)
end

#update(attrs = {}, &block) ⇒ Object



139
140
141
142
# File 'lib/active_record_base.rb', line 139

def update(attrs = {}, &block)
  attrs.each { |attr, value| send("#{attr}=", value) }
  save(&block)
end

#update_attribute(attr, value, &block) ⇒ Object



134
135
136
137
# File 'lib/active_record_base.rb', line 134

def update_attribute(attr, value, &block)
  send("#{attr}=", value)
  save(validate: false, &block)
end

#update_permitted?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/reactive_record/permissions.rb', line 17

def update_permitted?
  true
end

#view_permitted?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/reactive_record/permissions.rb', line 25

def view_permitted?(attribute)
  true
end