Skip to content

Compatibility for rails 3.2 #116

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

Closed
wants to merge 5 commits into from

Conversation

beatjoerg
Copy link
Contributor

Rails to serialize model names to symbols instead of strings. Fixed in more recent versions of Rails.

Rails to serialize model names to symbols instead of strings. Fixed in more recent versions of Rails.
@@ -336,7 +336,7 @@ def delete_document(options={})
def update_document(options={})
if changed_attributes = self.instance_variable_get(:@__changed_attributes)
attributes = if respond_to?(:as_indexed_json)
changed_attributes.select { |k,v| self.as_indexed_json.keys.include? k }
changed_attributes.select { |k,v| self.as_indexed_json.keys.map{|k| k.to_s}.include? k }
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if this could be improved a little. The original code called #as_indexed_json for each changed attribute in the model. If there were 10 changed attributes, there would be 10 calls to serialize the model. The code should probably only call #as_indexed_json once in order to get the list of keys.

The new code adds a map operation that converts all keys to strings, so if there were 10 keys to convert, and 10 attribute changes there would be 100 #to_s calls.

Symbol.#to_s creates a new String instance, while String.to_sym will always return the same symbol given the same String.

How about something like this?

indexed_symbols = self.as_indexed_json.keys.map(&:to_sym)
changed_attributes.select { |k,v| indexed_symbols.include? k.to_sym }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Your absolut right. I improved the code with commit b8aedf9

@karmi
Copy link
Contributor

karmi commented May 11, 2014

Yeah, thanks @AaronRustad, the main issue with repeatedly calling as_indexed_json was already reported in #85. The map(&:to_s) approach is correct, we need a better name than indexed_symbols, though.

@beatjoerg Thanks for the patch!, can you please try to figure out how you can add unit tests for your change? I wonder, by the way, what is going on in here, because the gem is tested against ActiveRecord 3.2, see the gemfile. I'd like to also ask you to consult the commit log, to see how the commits are worded and written -- I can always retouch them before merging, but it's also always nice when I have less to do :)

@beatjoerg
Copy link
Contributor Author

I have added a unit test as requested. I hope commit message confirms the rules.
The best to test would be an integration test, but I was unable to set up the test enviroment.

as_json(methods: [:tag_list])

returns in rails 3.2: {:tag_list=>["foo", "bar"]}
and in rails 4.1: {"tag_list"=>["foo", "bar"]}
but changes always returns {"tag_list"=>...}

@karmi
Copy link
Contributor

karmi commented May 11, 2014

@beatjoerg Great, thanks, I'll look at it next week.

The best to test would be an integration test, but I was unable to set up the test enviroment.

That's troubling, can you maybe open a new ticket with any errors etc. That should be all very easy -- the tests assume a testing cluster/node is running on 9250, but that can be configured of course.

…e miss because as_json returns symbols instead of strings in rails 3.2

Related: elastic#116
@beatjoerg
Copy link
Contributor Author

Just found my mistake and commited the integration test

@karmi
Copy link
Contributor

karmi commented May 12, 2014

@beatjoerg I don't think this particular issue warrants a separate, special integration test. Thanks for the patch!, I'll try processing it soon.

@karmi
Copy link
Contributor

karmi commented May 22, 2015

@beatjoerg I'm terribly sorry, I can see from git blame that I've already added the to_s conversion as part of 64111a2... Thanks for the patch and sorry for my oversight when doing that commit.

@karmi karmi closed this May 22, 2015
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.

3 participants