Sha256: 9c810722cbc45dd33622b34b1f20cf18d1d9726ef4243629da54dc3a832ee246

Contents?: true

Size: 1.07 KB

Versions: 16

Compression:

Stored size: 1.07 KB

Contents

module ActiveRecordDescendantAttributeAccessors

  def self.attr_accessor_instance_variables(obj)
    include_ancestor_methods = false
    
    ancestors_before_active_record_base = obj.class.ancestors.take_while { |klass| klass != ActiveRecord::Base }

    instance_methods_of_ancestors_before_active_record_base = ancestors_before_active_record_base
                                                                .map { |klass| klass.instance_methods(include_ancestor_methods) }
                                                                .flatten

    setter_methods_of_ancestors_before_active_record_base = instance_methods_of_ancestors_before_active_record_base
                                                                .select { |meth| meth.to_s.last == '=' } 

    possible_instance_variables_from_setter_methods = setter_methods_of_ancestors_before_active_record_base
                                                                .map { |meth| meth.to_s.prepend('@').chop.to_sym }

    obj.instance_variables & possible_instance_variables_from_setter_methods
  end

end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
purgatory-6.0.3 lib/purgatory/active_record_descendant_attribute_accessors.rb
purgatory-6.0.2 lib/purgatory/active_record_descendant_attribute_accessors.rb
purgatory-6.0.1 lib/purgatory/active_record_descendant_attribute_accessors.rb
purgatory-6.0.0 lib/purgatory/active_record_descendant_attribute_accessors.rb
purgatory-4.0.0 lib/purgatory/active_record_descendant_attribute_accessors.rb
purgatory-3.1.2 lib/purgatory/active_record_descendant_attribute_accessors.rb
purgatory-3.1.1 lib/purgatory/active_record_descendant_attribute_accessors.rb
purgatory-3.1.0 lib/purgatory/active_record_descendant_attribute_accessors.rb
purgatory-3.0.0 lib/purgatory/active_record_descendant_attribute_accessors.rb
purgatory-2.14.0 lib/purgatory/active_record_descendant_attribute_accessors.rb
purgatory-2.13.0 lib/purgatory/active_record_descendant_attribute_accessors.rb
purgatory-2.12.0 lib/purgatory/active_record_descendant_attribute_accessors.rb
purgatory-2.11.0 lib/purgatory/active_record_descendant_attribute_accessors.rb
purgatory-2.10.0 lib/purgatory/active_record_descendant_attribute_accessors.rb
purgatory-2.9.0 lib/purgatory/active_record_descendant_attribute_accessors.rb
purgatory-2.8.0 lib/purgatory/active_record_descendant_attribute_accessors.rb