Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional implementation details for key override #35

Merged
merged 1 commit into from
Aug 2, 2024
Merged
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
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,10 @@ json.flash flash.to_h
will render Layout first, then the template when `yield json` is used.

## Change key format
By default, keys are not formatted. If you want to change this behavior,
override it in an initializer:
By default, keys are not formatted. This is intentional. By being explicity with your keys,
it makes your views quicker and more easily searchable when working in Javascript land.

If you must change this behavior, override it in an initializer and cache the value:

```ruby
# default behavior
Expand All @@ -607,7 +609,15 @@ Props::BaseWithExtensions.class_eval do
#
# -> { "firstValue" => "first", "secondValue" => "second" }
def key_format(key)
key.to_s.camelize(:lower)
@key_cache ||= {}
@key_cache[key] ||= key.to_s.camelize(:lower)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this will prevent multiple transformations of key? looks good

@key_cache[key]
end

def result!
result = super
@key_cache = {}
result
end
end

Expand All @@ -618,7 +628,15 @@ Props::BaseWithExtensions.class_eval do
#
# -> { "first_value" => "first", "second_value" => "second" }
def key_format(key)
key.to_s.underscore
@key_cache ||= {}
@key_cache[key] ||= key.to_s.underscore
@key_cache[key]
end

def result!
result = super
@key_cache = {}
result
end
end
```
Expand Down
Loading