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

Fix adapter inflection bug for api -> API #1006

Merged
merged 2 commits into from
Jul 21, 2015

Conversation

bf4
Copy link
Member

@bf4 bf4 commented Jul 14, 2015

Bug: #993

Summary

  1. Added a (failing) test for when inflecting API (Rob McFadzean in Added a (failing) test for when inflecting API #997)
  2. sub s/API/Api

Some work done in here was moved in #1017

@bf4
Copy link
Member Author

bf4 commented Jul 14, 2015

cc @kurko @joaomdmoura your thoughts?

adapter_map.update(name.to_s.underscore => klass)
end

def get(adapter)
Copy link
Member

Choose a reason for hiding this comment

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

@bf4 this seems almost the same logic of the adapter method
We should probably remove it from there in order to use this new get method, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

You mean on the serializer? I can dedupe that in a new commit or pr

Copy link
Member

Choose a reason for hiding this comment

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

Yeap, it's exactly the same logic. IMHO it's related to this PR, check this out:

We can replace the actual code:

def self.adapter
  adapter_class = case config.adapter
  when Symbol
    ActiveModel::Serializer::Adapter.adapter_class(config.adapter)
  when Class
    config.adapter
  end
  unless adapter_class
    valid_adapters = Adapter.constants.map { |klass| ":#{klass.to_s.downcase}" }
    raise ArgumentError, "Unknown adapter: #{config.adapter}. Valid adapters are: #{valid_adapters}"
  end

  adapter_class
end

By something like:

def self.adapter
  ActiveModel::Serializer::Adapter.adapter_class(config.adapter)
end

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll take a look when I'm next at a computer. Do you like the new interface and behavior? Or have an alternative in mind?

Copy link
Member

Choose a reason for hiding this comment

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

cool! Yup, really liked it! If we can make it the only place to deal with this logic then I'm okay with it 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

Cool, I was a little uncertain about the naming

So, the two methods havr slightly different behavior

The one in the adapter would return nil if none found, but the one in the serializer raises an argument error

So, i was thinking of having get and get! on the adapter

Needs some thought where nil is desired

Copy link
Member Author

Choose a reason for hiding this comment

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

Added another commit to consider. I've been playing with the interface for success/failurre handling of finding the adapter by name and whether to find it by constants, or safe constantize, etc. So, this is for consideration as a way to do.

Copy link
Member

Choose a reason for hiding this comment

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

@bf4 it's seems to me nil is never desired. What are your thoughts about keep the argument error approach and remote get!?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good. I'd still like to allow for user defined failure handling

Copy link
Member Author

Choose a reason for hiding this comment

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

You'll notice that get can take a block to handle adapter not found, and is used by get!

        def get!(adapter)
          get(adapter) do |failure_message| fail ArgumentError, failure_message end
        end

@bf4 bf4 force-pushed the inflector-testing branch 2 times, most recently from 7f83aa1 to 9b33cf9 Compare July 17, 2015 19:08
@bf4 bf4 force-pushed the inflector-testing branch from 9b33cf9 to 4359026 Compare July 17, 2015 19:09
@bf4
Copy link
Member Author

bf4 commented Jul 17, 2015

It was easier to handle the simple case of api being inflected to API rather than Api via sub (not sure that's the most performant way. I didn't investigate), rather than have a whole bunch of changes around that either mutating ActiveSupport::Inflector.inflections(:en).acronyms or using I18n.with_locale(:ams) { }, or changing how we look up constants, etc.

@bf4
Copy link
Member Author

bf4 commented Jul 17, 2015

The stuff with the registered adapter I'll do in another PR, because it also brings up issues with autoloading

@joaomdmoura
Copy link
Member

@bf4 Will #1017 eliminate this? Because I'm not sure about it.

@bf4 bf4 changed the title Add registerable adapters; fix adapter inflection bug Fix adapter inflection bug for api -> API Jul 19, 2015
@bf4
Copy link
Member Author

bf4 commented Jul 19, 2015

No, this PR is still required. I just think the complexity I was introducing wasn't required for the range of inflection failures

@joaomdmoura
Copy link
Member

Okay @bf4. Good, it seems the most straightforward solution for me as well. I'm merging it.

joaomdmoura added a commit that referenced this pull request Jul 21, 2015
Fix adapter inflection bug for api -> API
@joaomdmoura joaomdmoura merged commit 6266b6a into rails-api:master Jul 21, 2015
@bf4 bf4 deleted the inflector-testing branch August 31, 2016 06:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants