Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning].

## [Unreleased]

### Changed

- **[BREAKING]** Enum types of namespaced models keep the namespace: `Course::Build` with a `state` enum gives `CourseBuildState`, not `BuildState`. Models with the same class name no longer overwrite each other's enums. ([@skryukov])

### Fixed

- Panko 1.0 support. ([@benjaminwols])
Expand Down
2 changes: 1 addition & 1 deletion lib/typelizer/model_plugins/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def enum_for(prop)
return unless model_class&.defined_enums&.key?(prop.column_name.to_s)

prop.enum = model_class.defined_enums[prop.column_name.to_s].keys
prop.enum_type_name = "#{model_class.name.demodulize}#{prop.column_name.to_s.camelize}"
prop.enum_type_name = "#{model_class.name.delete(":")}#{prop.column_name.to_s.camelize}"
prop.enum
end

Expand Down
11 changes: 7 additions & 4 deletions lib/typelizer/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@ def cleanup_stale_files(written_files, interfaces)
end

def collect_enums(interfaces)
interfaces
.flat_map(&:enum_types)
.uniq(&:enum_type_name)
.sort_by(&:enum_type_name)
interfaces.flat_map(&:enum_types).group_by(&:enum_type_name).sort.map do |name, enums|
if enums.map { |enum| enum.enum.to_set }.uniq.size > 1
Typelizer.logger.warn("Typelizer: enum type #{name} has conflicting values, only the first is generated")
end

enums.first
end
end

def write_enums(enums)
Expand Down
49 changes: 49 additions & 0 deletions spec/typelizer/namespaced_enum_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

RSpec.describe "Enums of namespaced models" do
let(:context) { Typelizer::WriterContext.new }

before do
stub_const("Course", Module.new)
stub_const("Project", Module.new)
end

# An interface exposing the `category` enum of a new model
def interface_for(model_name, values)
model = stub_const(model_name, Class.new(ApplicationRecord) do
self.table_name = "posts"
enum :category, values
end)

@resources = @resources.to_i + 1
context.interface_for(stub_const("Enum#{@resources}Resource", Class.new do
include ::Alba::Resource

helper Typelizer::DSL
typelize_from model
attributes :category
end))
end

def enum_names(*interfaces)
interfaces.flat_map(&:enum_types).map(&:enum_type_name)
end

it "keeps the namespace in the enum type name" do
course = interface_for("Course::Build", {pending: 0, done: 1})
project = interface_for("Project::Build", {queued: 0, failed: 1})

expect(enum_names(course, project)).to eq(%w[CourseBuildCategory ProjectBuildCategory])
end

it "warns when two different enums still get the same name" do
interfaces = [
interface_for("Course::Build", {pending: 0, done: 1}),
interface_for("CourseBuild", {queued: 0, failed: 1})
]

expect(Typelizer.logger).to receive(:warn).with(/CourseBuildCategory has conflicting values/)

Typelizer::Writer.new(context.writer_config).send(:collect_enums, interfaces)
end
end
Loading