class ActiveRecord::Base
ActiveRecord
monkey patches 1 - Setup synchronization after commits 2 - Update scope to accept different procs for server and client
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.
Attributes
reactive_record_association_keys[R]
acting_user[RW]
Public Class Methods
_synchromesh_scope_args_check(args)
click to toggle source
# 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
all()
click to toggle source
# 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)
click to toggle source
# File lib/active_record_base.rb, line 106 def all=(_collection) raise "NO LONGER IMPLEMENTED DOESNT PLAY WELL WITH SYNCHROMESH" end
belongs_to(attr_name, scope = nil, options = {})
click to toggle source
# 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
Also aliased as: belongs_to_without_reactive_record_add_is_method
belongs_to_without_reactive_record_add_is_method(attr_name, scope = nil, options = {})
Alias for: belongs_to
create(*args, &block)
click to toggle source
# File lib/active_record_base.rb, line 72 def create(*args, &block) new(*args).save(&block) end
default_scope(*args, &block)
click to toggle source
# 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
Also aliased as: pre_synchromesh_default_scope
do_not_synchronize()
click to toggle source
# File lib/active_record_base.rb, line 24 def do_not_synchronize @do_not_synchronize = true end
do_not_synchronize?()
click to toggle source
# File lib/active_record_base.rb, line 28 def do_not_synchronize? @do_not_synchronize end
finder_method(name, &block)
click to toggle source
# 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
method_missing(name, *args, &block)
click to toggle source
# 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
Also aliased as: pre_synchromesh_method_missing
public_columns_hash()
click to toggle source
# 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)
click to toggle source
# 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_aggregations()
click to toggle source
# 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_associations()
click to toggle source
# 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)
click to toggle source
# 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
scope(name, *args, &block)
click to toggle source
# 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
Also aliased as: pre_synchromesh_scope
server_method(name, opts = {}, &block)
click to toggle source
# File lib/active_record_base.rb, line 46 def server_method(name, opts = {}, &block) define_method name, &block end
unscoped()
click to toggle source
# 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
Public Instance Methods
<=>(other)
click to toggle source
# File lib/active_record_base.rb, line 167 def <=>(other) id.to_i <=> other.id.to_i end
all_changed?(*attributes)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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_permitted?()
click to toggle source
# File lib/reactive_record/permissions.rb, line 40 def create_permitted? false end
destroy_permitted?()
click to toggle source
# File lib/reactive_record/permissions.rb, line 48 def destroy_permitted? false end
do_not_synchronize?()
click to toggle source
# File lib/active_record_base.rb, line 130 def do_not_synchronize? self.class.do_not_synchronize? end
none_changed?(*attributes)
click to toggle source
# 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)
click to toggle source
# 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
synchromesh_after_change()
click to toggle source
# 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_create()
click to toggle source
# 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_destroy()
click to toggle source
# File lib/active_record_base.rb, line 148 def synchromesh_after_destroy return if do_not_synchronize? ReactiveRecord::Broadcast.after_commit :destroy, self end
update(attrs = {}, &block)
click to toggle source
# 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)
click to toggle source
# File lib/active_record_base.rb, line 157 def update_attribute(attr, value, &block) send("#{attr}=", value) save(validate: false, &block) end
update_permitted?()
click to toggle source
# File lib/reactive_record/permissions.rb, line 44 def update_permitted? false end
view_permitted?(attribute)
click to toggle source
# 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