From 4fe267b571e30646f026d48b6840b1451d97ae96 Mon Sep 17 00:00:00 2001 From: Ryan De Villa Date: Fri, 4 Jan 2019 14:54:43 -0500 Subject: [PATCH] DRY and remove dead code --- Gemfile.lock | 10 +-- activerecord_base_without_table.gemspec | 2 +- lib/active_record/base_without_table.rb | 65 ++++++-------------- spec/activerecord_base_without_table_spec.rb | 7 +-- 4 files changed, 26 insertions(+), 58 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index e4feb7a..4f1d214 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - activerecord_base_without_table (0.0.1) + activerecord_base_without_table (0.1.0) rails (~> 5.0.7) GEM @@ -51,17 +51,17 @@ GEM crass (1.0.4) diff-lcs (1.3) erubis (2.7.0) - globalid (0.4.1) + globalid (0.4.2) activesupport (>= 4.2.0) i18n (1.0.1) concurrent-ruby (~> 1.0) loofah (2.2.2) crass (~> 1.0.2) nokogiri (>= 1.5.9) - mail (2.7.0) + mail (2.7.1) mini_mime (>= 0.1.1) method_source (0.9.0) - mini_mime (1.0.0) + mini_mime (1.0.1) mini_portile2 (2.3.0) minitest (5.11.3) nio4r (2.3.1) @@ -116,7 +116,7 @@ GEM rspec-mocks (~> 3.5.0) rspec-support (~> 3.5.0) rspec-support (3.5.0) - sprockets (3.7.1) + sprockets (3.7.2) concurrent-ruby (~> 1.0) rack (> 1, < 3) sprockets-rails (3.2.1) diff --git a/activerecord_base_without_table.gemspec b/activerecord_base_without_table.gemspec index d5f1378..5d84b08 100644 --- a/activerecord_base_without_table.gemspec +++ b/activerecord_base_without_table.gemspec @@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__) # Describe your gem and declare its dependencies: Gem::Specification.new do |s| s.name = "activerecord_base_without_table" - s.version = "0.0.1" + s.version = "0.1.0" s.authors = ["Ryan De Villa"] s.email = ["ryand@nulogy.com"] s.homepage = "https://nulogy.com" diff --git a/lib/active_record/base_without_table.rb b/lib/active_record/base_without_table.rb index dcba195..63f4f6f 100644 --- a/lib/active_record/base_without_table.rb +++ b/lib/active_record/base_without_table.rb @@ -48,47 +48,29 @@ def attribute_names _default_attributes.keys.map(&:to_s) end - def column(name, sql_type = nil, default = nil, null = true) # :nodoc: - # do not use << in here. See: http://apidock.com/rails/Class/class_attribute - - # This is an emulation of the Rails 4.1 runtime behaviour. - # Please consider rewriting once we move to Rails 5. - mapped_sql_type = case sql_type - when :datetime - :date_time - when :datetime_point - :integer - when :enumerable - :value - else - sql_type - end.to_s - - cast_type = "ActiveRecord::Type::#{mapped_sql_type.camelize}".constantize.new + def column(name, sql_type = nil, default = nil, null = true) + cast_type = lookup_attribute_type(sql_type) decorated_type = attribute_type_decorations.apply(name, cast_type) define_attribute(name.to_s, decorated_type, default: default) end - def build_column_types - self.class.columns.reduce({}) do |acc, column| - acc.merge(column.name.to_s => column.sql_type_metadata) - end - end - - def lookup_column_type(sql_type) - # This is copy-pasted from ActiveRecord::BaseWithoutTable, please find another approach. - mapped_sql_type = case sql_type - when :datetime - :date_time - when :datetime_point - :integer - when :enumerable - :value - else - sql_type - end.to_s - "::ActiveRecord::Type::#{mapped_sql_type.camelize}".constantize.new + def lookup_attribute_type(sql_type) + # This is an emulation of the Rails 4.1 runtime behaviour. + # Please consider rewriting once we move to Rails 5.1. + mapped_sql_type = + case sql_type + when :datetime + :date_time + when :datetime_point + :integer + when :enumerable + :value + else + sql_type + end.to_s.camelize + + "::ActiveRecord::Type::#{mapped_sql_type}".constantize.new end def gettext_translation_for_attribute_name(attribute) @@ -100,16 +82,7 @@ def gettext_translation_for_attribute_name(attribute) if attribute.ends_with?("_id") humanize_class_name(attribute) else - "#{inheritance_class_owner(attribute)}|#{attribute.split('.').map!(&:humanize).join('|')}" - end - end - - def inheritance_class_owner(attribute) - superclass = self.superclass - if superclass.attribute_names.include?(attribute) - superclass.inheritance_class_owner(attribute) - else - self + "#{self}|#{attribute.split('.').map!(&:humanize).join('|')}" end end end diff --git a/spec/activerecord_base_without_table_spec.rb b/spec/activerecord_base_without_table_spec.rb index d0eee15..00aec3f 100644 --- a/spec/activerecord_base_without_table_spec.rb +++ b/spec/activerecord_base_without_table_spec.rb @@ -403,15 +403,10 @@ class Person < ActiveRecord::BaseWithoutTable validates_presence_of :name end - - class Employee < Person - column :salary, :integer - end end it 'supports gettext translations for attribute names' do - expect(BaseWithoutTableTests::Employee.gettext_translation_for_attribute_name(:salary)).to eq("BaseWithoutTableTests::Employee|Salary") - expect(BaseWithoutTableTests::Employee.gettext_translation_for_attribute_name(:name)).to eq("BaseWithoutTableTests::Person|Name") + expect(BaseWithoutTableTests::Person.gettext_translation_for_attribute_name(:name)).to eq("BaseWithoutTableTests::Person|Name") end end end