Skip to content

Commit

Permalink
Merge pull request #26 from itsameandrea/bugfix/associated-object-und…
Browse files Browse the repository at this point in the history
…erscore

Fix generated associated_object_path for CamelCase class names
  • Loading branch information
kaspth authored Oct 11, 2024
2 parents f69d15b + f022c42 commit 2dca445
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/generators/associated/associated_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def generate_associated_object_files

def connect_associated_object
record_file = "#{destination_root}/app/models/#{record_path}.rb"

raise "Record class '#{record_klass}' does not exist" unless File.exist?(record_file)

inject_into_class record_file, record_klass do
Expand All @@ -20,8 +21,8 @@ def connect_associated_object
# The `:name` argument can handle model names, but associated object class names aren't singularized.
# So these record and associated_object methods prevent that.
def record_path = record_klass.downcase.underscore
def record_klass = name.deconstantize
def record_klass = name.camelize.deconstantize

def associated_object_path = associated_object_class.downcase.underscore
def associated_object_class = name.demodulize
def associated_object_path = associated_object_class.underscore
def associated_object_class = name.camelize.demodulize
end
22 changes: 22 additions & 0 deletions test/lib/generators/associated_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,28 @@ class Organization
RUBY
end

test "connects record: Camelized name" do
run_generator ["Organization::SeatsManager"]

assert_file "app/models/organization.rb", <<~RUBY
class Organization
has_object :seats_manager
end
RUBY
end

test "connects record: lower_snake_case name" do
run_generator ["organization/seats_manager"]

assert_file "app/models/organization.rb", <<~RUBY
class Organization
has_object :seats_manager
end
RUBY
end



test "raises error if associated record doesn't exist" do
assert_raise RuntimeError do
run_generator ["Business::Monkey"]
Expand Down

0 comments on commit 2dca445

Please sign in to comment.