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/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.

Constant Summary

Constants included from ClassMethods

ClassMethods::SERVER_METHODS

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, define_attribute_methods, enum, find, find_by, inheritance_column, inheritance_column=, model_name, primary_key, primary_key=, server_methods

Methods included from InstanceMethods

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



63
64
65
66
67
68
69
70
# File 'lib/active_record_base.rb', line 63

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.



82
83
84
# File 'lib/reactive_record/permissions.rb', line 82

def reactive_record_association_keys
  @reactive_record_association_keys
end

Instance Attribute Details

#acting_userObject

Returns the value of attribute acting_user.



34
35
36
# File 'lib/reactive_record/permissions.rb', line 34

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



98
99
100
101
102
103
# File 'lib/reactive_record/permissions.rb', line 98

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



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

alias belongs_to_without_reactive_record_add_is_method belongs_to

.do_not_synchronizeObject



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

def do_not_synchronize
  @do_not_synchronize = true
end

.do_not_synchronize?Boolean

Returns:

  • (Boolean)


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

def do_not_synchronize?
  @do_not_synchronize
end

.pre_synchromesh_scopeObject



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

alias pre_synchromesh_scope scope

.public_columns_hashObject



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/reactive_record/active_record/public_columns_hash.rb', line 11

def self.public_columns_hash
  return @public_columns_hash if @public_columns_hash
  Hyperloop.public_model_directories.each do |dir|
    Dir.glob(Rails.root.join("#{dir}/*.rb")).each do |file|
      require_dependency(file)
    end
  end
  @public_columns_hash = {}
  descendants.each do |model|
    @public_columns_hash[model.name] = model.columns_hash rescue nil
  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



167
168
169
# File 'lib/active_record_base.rb', line 167

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

#allObject



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/active_record_base.rb', line 94

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



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

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

#all_changed?(*attributes) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
# File 'lib/reactive_record/permissions.rb', line 73

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

#any_changed?(*attributes) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
# File 'lib/reactive_record/permissions.rb', line 66

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



107
108
109
110
111
112
113
114
115
116
# File 'lib/reactive_record/permissions.rb', line 107

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



72
73
74
# File 'lib/active_record_base.rb', line 72

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

#create_permitted?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/reactive_record/permissions.rb', line 40

def create_permitted?
  false
end

#default_scope(*args, &block) ⇒ Object



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

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)


48
49
50
# File 'lib/reactive_record/permissions.rb', line 48

def destroy_permitted?
  false
end

#do_not_synchronize?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/active_record_base.rb', line 130

def do_not_synchronize?
  self.class.do_not_synchronize?
end

#finder_method(name) ⇒ Object



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

def finder_method(name, &block)
  singleton_class.send(:define_method, "_#{name}") do |*args|
    [block.call(*args)]
  end
  singleton_class.send(:define_method, name) do |*args|
    block.call(*args)
  end
end

#none_changed?(*attributes) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#only_changed?(*attributes) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
# File 'lib/reactive_record/permissions.rb', line 52

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_default_scopeObject



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

alias pre_synchromesh_default_scope default_scope

#pre_synchromesh_method_missingObject



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

alias pre_synchromesh_method_missing method_missing

#scope(name, *args) ⇒ Object



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

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

#server_method(name, opts = {}, &block) ⇒ Object



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

def server_method(name, opts = {}, &block)
  define_method name, &block
end

#synchromesh_after_changeObject



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

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

#synchromesh_after_createObject



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

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

#synchromesh_after_destroyObject



148
149
150
151
# File 'lib/active_record_base.rb', line 148

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

#unscopedObject



110
111
112
113
114
115
# File 'lib/active_record_base.rb', line 110

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

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



162
163
164
165
# File 'lib/active_record_base.rb', line 162

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

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



157
158
159
160
# File 'lib/active_record_base.rb', line 157

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

#update_permitted?Boolean

Returns:

  • (Boolean)


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

def update_permitted?
  false
end

#view_permitted?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


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

def view_permitted?(attribute)
  Hyperloop::InternalPolicy.accessible_attributes_for(self, acting_user).include? attribute.to_sym
end