Skip to content

Commit

Permalink
DRY and remove dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan De Villa committed Jan 14, 2019
1 parent 69274e8 commit 4fe267b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 58 deletions.
10 changes: 5 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion activerecord_base_without_table.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ["[email protected]"]
s.homepage = "https://nulogy.com"
Expand Down
65 changes: 19 additions & 46 deletions lib/active_record/base_without_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
7 changes: 1 addition & 6 deletions spec/activerecord_base_without_table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4fe267b

Please sign in to comment.