Skip to content

Commit

Permalink
fix(inheritance): allow models inheritance without bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ProGM committed Jul 13, 2023
1 parent 5178d14 commit 7357eba
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog
All notable changes to this project made by Monade Team are documented in this file. For info refer to [email protected]

## [0.1.2]
### Fixed
- Bug with inheritance: attributes declared in parent classes where not propagated to child classes

## [0.1.1]
### Fixed
- Name conflicts between columns and nosql attributes where not detected
Expand Down
2 changes: 1 addition & 1 deletion acts_as_nosql.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Gem::Specification.new do |s|
s.homepage = 'https://rubygems.org/gems/acts_as_nosql'
s.license = 'MIT'
s.add_dependency 'actionpack', ['>= 5', '< 8']
s.add_dependency 'activesupport', ['>= 5', '< 8']
s.add_dependency 'activerecord', ['>= 5', '< 8']
s.add_dependency 'activesupport', ['>= 5', '< 8']
s.add_development_dependency 'rspec', '~> 3'
s.add_development_dependency 'rubocop'
end
10 changes: 9 additions & 1 deletion lib/acts_as_nosql.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
# frozen_string_literal: true

require 'active_record'

# ActsAsNosql
#
# This gem allows to handle JSON and JSONB fields as if they are proper
# database columns, handling default values, type casting and simplifying
# validation.
# This module is the main entry point for the gem.
module ActsAsNosql
extend ActiveSupport::Concern
extend ActiveSupport::Autoload

class_methods do
attr_accessor :_acts_as_nosql_options
# cattr_accessor :_acts_as_nosql_options

def acts_as_nosql(field_name: nil)
@_acts_as_nosql_options = { field_name: field_name }

Expand Down
6 changes: 5 additions & 1 deletion lib/acts_as_nosql/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ module ActsAsNosql
class Attribute
attr_reader :name, :type, :default, :path, :type_caster

# @param [String, Symbol] name
# @param [String, Symbol, nil] type
# @param [Object, nil] default
# @param [Array<String, Symbol>, nil] path
def initialize(name, type: nil, default: nil, path: nil)
@name = name.to_s
@type = type
@default = default
@path = path&.map { |p| p.to_s }
@path = path&.map(&:to_s)
@type_caster = type ? "ActiveRecord::Type::#{type}".safe_constantize : nil
end

Expand Down
5 changes: 5 additions & 0 deletions lib/acts_as_nosql/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ def nosql_attr(*names, type: nil, default: nil, path: nil)
end
end

def inherited(subclass)
subclass._acts_as_nosql_options = self._acts_as_nosql_options.deep_dup
super
end

def nosql_attributes
self._acts_as_nosql_options[:attributes] ||= {}
end
Expand Down
1 change: 1 addition & 0 deletions lib/acts_as_nosql/querying.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true

module ActsAsNosql
module Querying
Expand Down
4 changes: 3 additions & 1 deletion lib/acts_as_nosql/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ActsAsNosql
VERSION = '0.1.1'.freeze
VERSION = '0.1.2'.freeze
end
22 changes: 22 additions & 0 deletions spec/acts_as_nosql/inheritance_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'spec_helper'

describe 'Inheritance' do
subject { InheritedSetting.new }
it 'inherits all methods from Settings' do
expect(subject).to respond_to(:user_auth_token)
expect(subject).to respond_to(:user_auth_token=)

subject.user_auth_token = '3'
expect(subject.user_auth_token).to eq('3')
subject.save!
expect do
expect(InheritedSetting.where(user_auth_token: '3')).to eq([subject])
end.to_not raise_error
end

it 'can be estended without affecting the parent' do
InheritedSetting.nosql_attrs :hello, type: :String
expect(subject).to respond_to(:hello)
expect(Setting.new).not_to respond_to(:hello)
end
end
9 changes: 9 additions & 0 deletions spec/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class Setting < ActiveRecord::Base
nosql_attrs :user_auth_providers, type: Array, default: [], path: [:user, :auth, :providers]
end

class InheritedSetting < Setting
end

module Schema
def self.create
ActiveRecord::Migration.verbose = false
Expand All @@ -60,6 +63,12 @@ def self.create
t.json :config
t.timestamps null: false
end

create_table :inherited_settings, force: true do |t|
t.string :title
t.json :config
t.timestamps null: false
end
end
end
end

0 comments on commit 7357eba

Please sign in to comment.