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 has one class name #1028

Merged

Conversation

dmitry-solomadin
Copy link
Contributor

@dmitry-solomadin dmitry-solomadin commented Nov 20, 2017

Related issue: #951

Currently, when passing class_name option to has_one field it does not infer correct class name for the dashboard which results in exception.
This PR modifies has_one class to use associative to get the correct dashboard name.
Additionally, it fixes permitted_attributes method, which required me to modify the signature and pass options from Deferred attribute.

In scope of this I've also removed old locale keys for has_one.not_supported string.

/r @nickcharlton @tysongach

@@ -11,7 +11,7 @@ def associated_resource_grouped_options
end
end

def self.permitted_attribute(attr)
def self.permitted_attribute(attr, options=nil)

Choose a reason for hiding this comment

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

Unused method argument - options. If it's necessary, use _ or _options as an argument name to indicate that it won't be used.
Surrounding space missing in default value assignment.

private

def resolver
@resolver ||= Administrate::ResourceResolver.new("admin/#{associated_class_name}")

Choose a reason for hiding this comment

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

Line is too long. [90/80]

options.fetch(:class_name, attr.to_s.singularize.camelcase)
else
attr
end

Choose a reason for hiding this comment

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

Trailing whitespace detected.

end

def self.permitted_attribute(attr)
def self.permitted_attribute(attr, options=nil)

Choose a reason for hiding this comment

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

Surrounding space missing in default value assignment.

@@ -7,8 +7,8 @@ module Field
class HasMany < Associative
DEFAULT_LIMIT = 5

def self.permitted_attribute(attribute)
{ "#{attribute.to_s.singularize}_ids".to_sym => [] }
def self.permitted_attribute(attr, options=nil)

Choose a reason for hiding this comment

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

Unused method argument - options. If it's necessary, use _ or _options as an argument name to indicate that it won't be used.
Surrounding space missing in default value assignment.

def permitted_attribute(attr)
options.fetch(:foreign_key, deferred_class.permitted_attribute(attr))
def permitted_attribute(attr, options=nil)
self.options.fetch(:foreign_key, deferred_class.permitted_attribute(attr, self.options))

Choose a reason for hiding this comment

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

Line is too long. [96/80]

@@ -25,8 +25,8 @@ def searchable?
options.fetch(:searchable, deferred_class.searchable?)
end

def permitted_attribute(attr)
options.fetch(:foreign_key, deferred_class.permitted_attribute(attr))
def permitted_attribute(attr, options=nil)

Choose a reason for hiding this comment

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

Unused method argument - options. If it's necessary, use _ or _options as an argument name to indicate that it won't be used.
Surrounding space missing in default value assignment.

@@ -3,7 +3,7 @@
module Administrate
module Field
class BelongsTo < Associative
def self.permitted_attribute(attr)
def self.permitted_attribute(attr, options=nil)

Choose a reason for hiding this comment

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

Unused method argument - options. If it's necessary, use _ or _options as an argument name to indicate that it won't be used.
Surrounding space missing in default value assignment.

@@ -24,7 +24,7 @@ def initialize(attribute, data, page, options = {})
@options = options
end

def self.permitted_attribute(attr)
def self.permitted_attribute(attr, options=nil)

Choose a reason for hiding this comment

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

Unused method argument - options. If it's necessary, use _ or _options as an argument name to indicate that it won't be used.
Surrounding space missing in default value assignment.

@@ -11,7 +11,7 @@ def associated_resource_grouped_options
end
end

def self.permitted_attribute(attr)
def self.permitted_attribute(attr, options=nil)

Choose a reason for hiding this comment

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

Unused method argument - options. If it's necessary, use _ or _options as an argument name to indicate that it won't be used.
Surrounding space missing in default value assignment.

private

def resolver
@resolver ||= Administrate::ResourceResolver.new("admin/#{associated_class_name}")

Choose a reason for hiding this comment

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

Line is too long. [90/80]

options.fetch(:class_name, attr.to_s.singularize.camelcase)
else
attr
end

Choose a reason for hiding this comment

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

Trailing whitespace detected.

end

def self.permitted_attribute(attr)
def self.permitted_attribute(attr, options=nil)

Choose a reason for hiding this comment

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

Surrounding space missing in default value assignment.

@@ -7,8 +7,8 @@ module Field
class HasMany < Associative
DEFAULT_LIMIT = 5

def self.permitted_attribute(attribute)
{ "#{attribute.to_s.singularize}_ids".to_sym => [] }
def self.permitted_attribute(attr, options=nil)

Choose a reason for hiding this comment

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

Unused method argument - options. If it's necessary, use _ or _options as an argument name to indicate that it won't be used.
Surrounding space missing in default value assignment.

def permitted_attribute(attr)
options.fetch(:foreign_key, deferred_class.permitted_attribute(attr))
def permitted_attribute(attr, options=nil)
self.options.fetch(:foreign_key, deferred_class.permitted_attribute(attr, self.options))

Choose a reason for hiding this comment

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

Line is too long. [96/80]

@@ -25,8 +25,8 @@ def searchable?
options.fetch(:searchable, deferred_class.searchable?)
end

def permitted_attribute(attr)
options.fetch(:foreign_key, deferred_class.permitted_attribute(attr))
def permitted_attribute(attr, options=nil)

Choose a reason for hiding this comment

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

Unused method argument - options. If it's necessary, use _ or _options as an argument name to indicate that it won't be used.
Surrounding space missing in default value assignment.

@@ -3,7 +3,7 @@
module Administrate
module Field
class BelongsTo < Associative
def self.permitted_attribute(attr)
def self.permitted_attribute(attr, options=nil)

Choose a reason for hiding this comment

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

Unused method argument - options. If it's necessary, use _ or _options as an argument name to indicate that it won't be used.
Surrounding space missing in default value assignment.

@@ -24,7 +24,7 @@ def initialize(attribute, data, page, options = {})
@options = options
end

def self.permitted_attribute(attr)
def self.permitted_attribute(attr, options=nil)

Choose a reason for hiding this comment

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

Unused method argument - options. If it's necessary, use _ or _options as an argument name to indicate that it won't be used.
Surrounding space missing in default value assignment.

options.fetch(:foreign_key, deferred_class.permitted_attribute(attr))
def permitted_attribute(attr, _options = nil)
self.options.fetch(:foreign_key,
deferred_class.permitted_attribute(attr, self.options))

Choose a reason for hiding this comment

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

Redundant self detected.

def permitted_attribute(attr)
options.fetch(:foreign_key, deferred_class.permitted_attribute(attr))
def permitted_attribute(attr, _options = nil)
self.options.fetch(:foreign_key,

Choose a reason for hiding this comment

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

Redundant self detected.

options.fetch(:foreign_key, deferred_class.permitted_attribute(attr))
def permitted_attribute(attr, _options = nil)
self.options.fetch(:foreign_key,
deferred_class.permitted_attribute(attr, self.options))

Choose a reason for hiding this comment

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

Redundant self detected.

def permitted_attribute(attr)
options.fetch(:foreign_key, deferred_class.permitted_attribute(attr))
def permitted_attribute(attr, _options = nil)
self.options.fetch(:foreign_key,

Choose a reason for hiding this comment

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

Redundant self detected.

@dmitry-solomadin dmitry-solomadin force-pushed the ds-fix-has-one-class-name branch from cbb89ba to 8df9a57 Compare November 25, 2017 20:50
field_name = 'seo_meta_tag'
attributes = field.permitted_attribute(field_name)
expect(attributes[:"#{field_name}_attributes"]).
to eq([:meta_title, :meta_description, :id])

Choose a reason for hiding this comment

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

Use %i or %I for an array of symbols.

field = Administrate::Field::Deferred.
new(Administrate::Field::HasOne.with_options(class_name: :product_meta_tag))

field_name = 'seo_meta_tag'

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

context 'with custom class_name' do
it "returns attributes from correct dashboard" do
field = Administrate::Field::Deferred.
new(Administrate::Field::HasOne.with_options(class_name: :product_meta_tag))

Choose a reason for hiding this comment

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

Line is too long. [86/80]

end

describe ".permitted_attribute" do
context 'with custom class_name' do

Choose a reason for hiding this comment

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

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

@dmitry-solomadin
Copy link
Contributor Author

Added some tests. PR is ready for review
/cc @nickcharlton @tysongach

Copy link

@etagwerker etagwerker left a comment

Choose a reason for hiding this comment

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

@dmitry-solomadin This is a great addition to the project. We are using it in one of our projects and it works. Thank you! 👍

@nickcharlton
Copy link
Member

Hi @dmitry-solomadin! Thank you for opening this!

As I've taken a while to look at it, it's fallen behind a bit. Would you mind rebasing it (it's mostly translations, it seems…)?

@@ -20,7 +20,7 @@
deferred.permitted_attribute(:foo)

expect(Administrate::Field::String).
to have_received(:permitted_attribute).with(:foo)
to have_received(:permitted_attribute).with(:foo, {})
Copy link
Member

Choose a reason for hiding this comment

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

If the method is providing a default (nil), is it valuable for us to be providing an empty hash here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, however in that particular case we are testing this method on String field in scope of Deferred attribute, in deferred.rb the options that we pass are actually initialized with {} by default

@dmitry-solomadin dmitry-solomadin force-pushed the ds-fix-has-one-class-name branch from 8c61b73 to 4b83819 Compare December 20, 2017 02:31
@dmitry-solomadin
Copy link
Contributor Author

@nickcharlton Hey Nick, rebased the branch off latest master, should be good now.
Let me know if you have any other concerns.

@dmitry-solomadin
Copy link
Contributor Author

Looks like some tests on master are failing the same ones are failing on my branch

@nickcharlton
Copy link
Member

Great, thanks! The failures are unrelated to your work, so I'm ignoring them until I get some time on Friday to look at them. I'll merge this now!

@nickcharlton nickcharlton merged commit 9f693cc into thoughtbot:master Dec 20, 2017
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.

4 participants