lucky icon indicating copy to clipboard operation
lucky copied to clipboard

lucky gen.resource.browser ignores config/inflector.cr overrides

Open btihen opened this issue 3 years ago • 6 comments

Describe the bug

Lucky 0.28.0

First I setup a config/inflector.cr file with:

require "wordsmith"
Wordsmith::Inflector.inflections.irregular("person", "persons")
Wordsmith::Inflector.inflections.uncountable("staff")

When I type:

lucky gen.resource.browser Person name:String

I still get people instead of persons:

Created CreatePeople::V20210811201528 in ./db/migrations/20210811201528_create_people.cr
Generated Person in ./src/models/person.cr
Generated SavePerson in ./src/operations/save_person.cr
Generated DeletePerson in ./src/operations/delete_person.cr
Generated PersonQuery in ./src/queries/person_query.cr
Generated People::Index in ./src/actions/people/index.cr
Generated People::Show in ./src/actions/people/show.cr
Generated People::New in ./src/actions/people/new.cr
Generated People::Create in ./src/actions/people/create.cr
Generated People::Edit in ./src/actions/people/edit.cr
Generated People::Update in ./src/actions/people/update.cr
Generated People::Delete in ./src/actions/people/delete.cr
Generated People::IndexPage in ./src/pages/people/index_page.cr
Generated People::ShowPage in ./src/pages/people/show_page.cr
Generated People::NewPage in ./src/pages/people/new_page.cr
Generated People::EditPage in ./src/pages/people/edit_page.cr
Generated People::FormFields in ./src/components/people/form_fields.cr

And when I type:

lucky gen.resource.browser Staff name:String

And it generates a route of staffs:

Created CreateStaffs::V20210811201213 in ./db/migrations/20210811201213_create_staffs.cr
Generated Staff in ./src/models/staff.cr
Generated SaveStaff in ./src/operations/save_staff.cr
Generated DeleteStaff in ./src/operations/delete_staff.cr
Generated StaffQuery in ./src/queries/staff_query.cr
Generated Staffs::Index in ./src/actions/staffs/index.cr
Generated Staffs::Show in ./src/actions/staffs/show.cr
Generated Staffs::New in ./src/actions/staffs/new.cr
Generated Staffs::Create in ./src/actions/staffs/create.cr
Generated Staffs::Edit in ./src/actions/staffs/edit.cr
Generated Staffs::Update in ./src/actions/staffs/update.cr
Generated Staffs::Delete in ./src/actions/staffs/delete.cr
Generated Staffs::IndexPage in ./src/pages/staffs/index_page.cr
Generated Staffs::ShowPage in ./src/pages/staffs/show_page.cr
Generated Staffs::NewPage in ./src/pages/staffs/new_page.cr
Generated Staffs::EditPage in ./src/pages/staffs/edit_page.cr
Generated Staffs::FormFields in ./src/components/staffs/form_fields.cr

However, when I test with lucky exec I get the expected results:

lucky exec
# then when vim or nano opens you can enter something like:
require "../../src/app.cr"
include Lucky::TextHelpers
pp pluralize(2, "staff")
pp pluralize(2, "person")

I get:

"2 staff"
"2 persons"

To Reproduce Steps to reproduce the behavior:

  1. Generate a default Lucky project 0.28.0
  2. create a config/inflector.cr file with the contents:
require "wordsmith"
Wordsmith::Inflector.inflections.uncountable("staff")
Wordsmith::Inflector.inflections.irregular("person", "persons")
  1. generate lucky gen.resource.browser Staff name:String lucky gen.resource.browser Person name:String
  2. you will get resources and routes that are the default in lucky and not what is defined in config/inflector.cr

Expected behavior

When I define inflectors - I expect lucky generators to use the same inflections as those defined in config/inflector.cr and those used when app.cr is loaded.

Screenshots/code already provided in the problem description

Versions (please complete the following information):

  • Lucky version 0.28.0:
  • Crystal version 1.1.1:
  • OS: MacOS 11.5.1

Additional context If someone can explain when and how lucky and the lucky.gen load dependencies and configurations - especially wordsmith -- I will be glad to submit a PR.

btihen avatar Aug 12 '21 17:08 btihen

oh, good catch. I think this task is precompiled... Can you try doing crystal build tasks.cr -o ./bin/lucky then do ./bin/lucky gen.resource.browser Person name:String? My guess is because it's precompiled, it doesn't know that you've overridden that value.

jwoertink avatar Aug 12 '21 18:08 jwoertink

Hmm - didn't help. It looks like it doesn't read my config/inflector.cr at all when pre-compiling.

Recompiling worked without error

✔ ~/devel/marpori/learning/crystal_frameworks/lucky/animals [main L|✚ 6…17] 

but it generates the same results.

20:18 $ lucky gen.resource.browser Person name:String
Created CreatePeople::V20210812181914 in ./db/migrations/20210812181914_create_people.cr
Generated Person in ./src/models/person.cr
Generated SavePerson in ./src/operations/save_person.cr
Generated DeletePerson in ./src/operations/delete_person.cr
Generated PersonQuery in ./src/queries/person_query.cr
Generated People::Index in ./src/actions/people/index.cr
Generated People::Show in ./src/actions/people/show.cr
Generated People::New in ./src/actions/people/new.cr
Generated People::Create in ./src/actions/people/create.cr
Generated People::Edit in ./src/actions/people/edit.cr
Generated People::Update in ./src/actions/people/update.cr
Generated People::Delete in ./src/actions/people/delete.cr
Generated People::IndexPage in ./src/pages/people/index_page.cr
Generated People::ShowPage in ./src/pages/people/show_page.cr
Generated People::NewPage in ./src/pages/people/new_page.cr
Generated People::EditPage in ./src/pages/people/edit_page.cr
Generated People::FormFields in ./src/components/people/form_fields.cr

20:19 $ lucky gen.resource.browser Staff name:String

Created CreateStaffs::V20210812181926 in ./db/migrations/20210812181926_create_staffs.cr
Generated Staff in ./src/models/staff.cr
Generated SaveStaff in ./src/operations/save_staff.cr
Generated DeleteStaff in ./src/operations/delete_staff.cr
Generated StaffQuery in ./src/queries/staff_query.cr
Generated Staffs::Index in ./src/actions/staffs/index.cr
Generated Staffs::Show in ./src/actions/staffs/show.cr
Generated Staffs::New in ./src/actions/staffs/new.cr
Generated Staffs::Create in ./src/actions/staffs/create.cr
Generated Staffs::Edit in ./src/actions/staffs/edit.cr
Generated Staffs::Update in ./src/actions/staffs/update.cr
Generated Staffs::Delete in ./src/actions/staffs/delete.cr
Generated Staffs::IndexPage in ./src/pages/staffs/index_page.cr
Generated Staffs::ShowPage in ./src/pages/staffs/show_page.cr
Generated Staffs::NewPage in ./src/pages/staffs/new_page.cr
Generated Staffs::EditPage in ./src/pages/staffs/edit_page.cr
Generated Staffs::FormFields in ./src/components/staffs/form_fields.cr

btihen avatar Aug 12 '21 18:08 btihen

OK - this seemed to work - respect the config files (avoid precompiling) - I guess this is workable if documented somewhere. But I would hope for a more default based solution when configuring an enviornment.

$ rm -rf lib && rm -rf bin

$ SKIP_LUCKY_TASK_PRECOMPILATION=true shards install

$ ./script/setup

$ lucky gen.resource.browser Staff name:String

compiling ...
Created CreateStaff::V20210812185142 in ./db/migrations/20210812185142_create_staff.cr
Generated Staff in ./src/models/staff.cr
Generated SaveStaff in ./src/operations/save_staff.cr
Generated DeleteStaff in ./src/operations/delete_staff.cr
Generated StaffQuery in ./src/queries/staff_query.cr
Generated Staff::Index in ./src/actions/staff/index.cr
Generated Staff::Show in ./src/actions/staff/show.cr
Generated Staff::New in ./src/actions/staff/new.cr
Generated Staff::Create in ./src/actions/staff/create.cr
Generated Staff::Edit in ./src/actions/staff/edit.cr
Generated Staff::Update in ./src/actions/staff/update.cr
Generated Staff::Delete in ./src/actions/staff/delete.cr
Generated Staff::IndexPage in ./src/pages/staff/index_page.cr
Generated Staff::ShowPage in ./src/pages/staff/show_page.cr
Generated Staff::NewPage in ./src/pages/staff/new_page.cr
Generated Staff::EditPage in ./src/pages/staff/edit_page.cr
Generated Staff::FormFields in ./src/components/staff/form_fields.cr

btihen avatar Aug 12 '21 18:08 btihen

I can add this to a readme or to the doc website - if there is an appropriate place. I'll write a little blog for my own memory - I'll put that link in here when I have published it.

btihen avatar Aug 12 '21 19:08 btihen

Here is another format for this solution: https://btihen.me/post_crystal/lucky_0_28_0_inflections/

btihen avatar Aug 15 '21 19:08 btihen

Thanks for the post @btihen helped me with the same issue. I also had to update the model. In my case its for a set of vrfs

Unhandled exception: Vrf wants to use the 'vrves' table but it is missing.

# from
class Vrf < BaseModel
  table do
    # columns
  end
end

# to
class Vrf < BaseModel
  table :vrfs do
    # columns
  end
end

bwks avatar Aug 26 '21 11:08 bwks