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 AMS warnings #2019

Merged
merged 2 commits into from
Jan 7, 2017
Merged

Fix AMS warnings #2019

merged 2 commits into from
Jan 7, 2017

Conversation

bf4
Copy link
Member

@bf4 bf4 commented Jan 6, 2017

Extracted from #2017

  • Fix thor warning to set type: :boolean, was setting { banner: "" } ref Fix warning on Thor #2008

    require "rails/generators/rails/model/model_generator"
    
    module Rails
     module Generators
       class ResourceGenerator < ModelGenerator # :nodoc:
         include ResourceHelpers
    
          hook_for :resource_controller, required: true do |controller|
           invoke controller, [ controller_name, options[:actions] ]
         end
    
          class_option :actions, type: :array, banner: "ACTION ACTION",
    default: [],
                                desc: "Actions for the resource controller"
    
          hook_for :resource_route, required: true
       end
     end end
    #  .bundle/ruby/2.2.0/bundler/gems/rails-4c5f1bc9d45e/railties/lib/rails/generators/base.rb
         # Invoke a generator based on the value supplied by the user to the
         # given option named "name". A class option is created when this
    method
         # is invoked and you can set a hash to customize it.
         #
         # ==== Examples
         #
         #   module Rails::Generators
         #     class ControllerGenerator < Base
         #       hook_for :test_framework, aliases: "-t"
         #     end
         #   end
         #
         # The example above will create a test framework option and will
    invoke
         # a generator based on the user supplied value.
         #
         # For example, if the user invoke the controller generator as:
         #
         #   rails generate controller Account --test-framework=test_unit
         #
         # The controller generator will then try to invoke the following
    generators:
         #
         #   "rails:test_unit", "test_unit:controller", "test_unit"
         #
         # Notice that "rails:generators:test_unit" could be loaded as well,
    what
         # Rails looks for is the first and last parts of the namespace. This
    is what
         # allows any test framework to hook into Rails as long as it provides
    any
         # of the hooks above.
         #
         # ==== Options
         #
         # The first and last part used to find the generator to be invoked are
         # guessed based on class invokes hook_for, as noticed in the example
    above.
         # This can be customized with two options: :in and :as.
         #
         # Let's suppose you are creating a generator that needs to invoke the
         # controller generator from test unit. Your first attempt is:
         #
         #   class AwesomeGenerator < Rails::Generators::Base
         #     hook_for :test_framework
         #   end
         #
         # The lookup in this case for test_unit as input is:
         #
         #   "test_unit:awesome", "test_unit"
         #
         # Which is not the desired lookup. You can change it by providing the
         # :as option:
         #
         #   class AwesomeGenerator < Rails::Generators::Base
         #     hook_for :test_framework, as: :controller
         #   end
         #
         # And now it will look up at:
         #
         #   "test_unit:controller", "test_unit"
         #
         # Similarly, if you want it to also look up in the rails namespace,
    you
         # just need to provide the :in value:
         #
         #   class AwesomeGenerator < Rails::Generators::Base
         #     hook_for :test_framework, in: :rails, as: :controller
         #   end
         #
         # And the lookup is exactly the same as previously:
         #
         #   "rails:test_unit", "test_unit:controller", "test_unit"
         #
         # ==== Switches
         #
         # All hooks come with switches for user interface. If you do not want
         # to use any test framework, you can do:
         #
         #   rails generate controller Account --skip-test-framework
         #
         # Or similarly:
         #
         #   rails generate controller Account --no-test-framework
         #
         # ==== Boolean hooks
         #
         # In some cases, you may want to provide a boolean hook. For example,
    webrat
         # developers might want to have webrat available on controller
    generator.
         # This can be achieved as:
         #
         #   Rails::Generators::ControllerGenerator.hook_for :webrat, type:
    :boolean
         #
         # Then, if you want webrat to be invoked, just supply:
         #
         #   rails generate controller Account --webrat
         #
         # The hooks lookup is similar as above:
         #
         #   "rails:generators:webrat", "webrat:generators:controller",
    "webrat"
         #
         # ==== Custom invocations
         #
         # You can also supply a block to hook_for to customize how the hook is
         # going to be invoked. The block receives two arguments, an instance
         # of the current class and the class to be invoked.
         #
         # For example, in the resource generator, the controller should be
    invoked
         # with a pluralized class name. But by default it is invoked with the
    same
         # name as the resource generator, which is singular. To change this,
    we
         # can give a block to customize how the controller can be invoked.
         #
         #   hook_for :resource_controller do |instance, controller|
         #     instance.invoke controller, [ instance.name.pluralize ]
         #   end
         #
         def self.hook_for(*names, &block)
           options = names.extract_options!
           in_base = options.delete(:in) || base_name
           as_hook = options.delete(:as) || generator_name
    
            names.each do |name|
             unless class_options.key?(name)
               defaults = if options[:type] == :boolean
                 {}
               elsif [true, false].include?(default_value_for_option(name,
    options))
                 { banner: "" }
               else
                 { desc: "#{name.to_s.humanize} to be invoked", banner: "NAME"
    }
               end
    
                class_option(name, defaults.merge!(options))
             end
    
              hooks[name] = [ in_base, as_hook ]
             invoke_from_option(name, options, &block)
           end
         end
     # .bundle/ruby/2.2.0/gems/thor-0.19.4/lib/thor/parser/option.rb:113:in
    `validate!'
       #   parse :foo => true
       #   #=> Option foo with default value true and type boolean
       #
       # The valid types are :boolean, :numeric, :hash, :array and :string. If
    none
       # is given a default type is assumed. This default type accepts
    arguments as
       # string (--foo=value) or booleans (just --foo).
       #
       # By default all options are optional, unless :required is given.
       def validate_default_type!
         default_type = case @default
         when nil
           return
         when TrueClass, FalseClass
           required? ? :string : :boolean
         when Numeric
           :numeric
         when Symbol
           :string
         when Hash, Array, String
           @default.class.name.downcase.to_sym
         end
    
          # TODO: This should raise an ArgumentError in a future version of
    Thor
         if default_type != @type
           warn "Expected #{@type} default value for '#{switch_name}'; got
    #{@default.inspect} (#{default_type})"
         end
       end

@mention-bot
Copy link

@bf4, thanks for your PR! By analyzing the history of the files in this pull request, we identified @steveklabnik, @Eric-Guo and @dgynn to be potential reviewers.

```
require "rails/generators/rails/model/model_generator"

module Rails
  module Generators
    class ResourceGenerator < ModelGenerator # :nodoc:
      include ResourceHelpers

      hook_for :resource_controller, required: true do |controller|
        invoke controller, [ controller_name, options[:actions] ]
      end

      class_option :actions, type: :array, banner: "ACTION ACTION", default: [],
                             desc: "Actions for the resource controller"

      hook_for :resource_route, required: true
    end
  end
end
```

```
 # .bundle/ruby/2.2.0/bundler/gems/rails-4c5f1bc9d45e/railties/lib/rails/generators/base.rb
      # Invoke a generator based on the value supplied by the user to the
      # given option named "name". A class option is created when this method
      # is invoked and you can set a hash to customize it.
      #
      # ==== Examples
      #
      #   module Rails::Generators
      #     class ControllerGenerator < Base
      #       hook_for :test_framework, aliases: "-t"
      #     end
      #   end
      #
      # The example above will create a test framework option and will invoke
      # a generator based on the user supplied value.
      #
      # For example, if the user invoke the controller generator as:
      #
      #   rails generate controller Account --test-framework=test_unit
      #
      # The controller generator will then try to invoke the following generators:
      #
      #   "rails:test_unit", "test_unit:controller", "test_unit"
      #
      # Notice that "rails:generators:test_unit" could be loaded as well, what
      # Rails looks for is the first and last parts of the namespace. This is what
      # allows any test framework to hook into Rails as long as it provides any
      # of the hooks above.
      #
      # ==== Options
      #
      # The first and last part used to find the generator to be invoked are
      # guessed based on class invokes hook_for, as noticed in the example above.
      # This can be customized with two options: :in and :as.
      #
      # Let's suppose you are creating a generator that needs to invoke the
      # controller generator from test unit. Your first attempt is:
      #
      #   class AwesomeGenerator < Rails::Generators::Base
      #     hook_for :test_framework
      #   end
      #
      # The lookup in this case for test_unit as input is:
      #
      #   "test_unit:awesome", "test_unit"
      #
      # Which is not the desired lookup. You can change it by providing the
      # :as option:
      #
      #   class AwesomeGenerator < Rails::Generators::Base
      #     hook_for :test_framework, as: :controller
      #   end
      #
      # And now it will look up at:
      #
      #   "test_unit:controller", "test_unit"
      #
      # Similarly, if you want it to also look up in the rails namespace, you
      # just need to provide the :in value:
      #
      #   class AwesomeGenerator < Rails::Generators::Base
      #     hook_for :test_framework, in: :rails, as: :controller
      #   end
      #
      # And the lookup is exactly the same as previously:
      #
      #   "rails:test_unit", "test_unit:controller", "test_unit"
      #
      # ==== Switches
      #
      # All hooks come with switches for user interface. If you do not want
      # to use any test framework, you can do:
      #
      #   rails generate controller Account --skip-test-framework
      #
      # Or similarly:
      #
      #   rails generate controller Account --no-test-framework
      #
      # ==== Boolean hooks
      #
      # In some cases, you may want to provide a boolean hook. For example, webrat
      # developers might want to have webrat available on controller generator.
      # This can be achieved as:
      #
      #   Rails::Generators::ControllerGenerator.hook_for :webrat, type: :boolean
      #
      # Then, if you want webrat to be invoked, just supply:
      #
      #   rails generate controller Account --webrat
      #
      # The hooks lookup is similar as above:
      #
      #   "rails:generators:webrat", "webrat:generators:controller", "webrat"
      #
      # ==== Custom invocations
      #
      # You can also supply a block to hook_for to customize how the hook is
      # going to be invoked. The block receives two arguments, an instance
      # of the current class and the class to be invoked.
      #
      # For example, in the resource generator, the controller should be invoked
      # with a pluralized class name. But by default it is invoked with the same
      # name as the resource generator, which is singular. To change this, we
      # can give a block to customize how the controller can be invoked.
      #
      #   hook_for :resource_controller do |instance, controller|
      #     instance.invoke controller, [ instance.name.pluralize ]
      #   end
      #
      def self.hook_for(*names, &block)
        options = names.extract_options!
        in_base = options.delete(:in) || base_name
        as_hook = options.delete(:as) || generator_name

        names.each do |name|
          unless class_options.key?(name)
            defaults = if options[:type] == :boolean
              {}
            elsif [true, false].include?(default_value_for_option(name, options))
              { banner: "" }
            else
              { desc: "#{name.to_s.humanize} to be invoked", banner: "NAME" }
            end

            class_option(name, defaults.merge!(options))
          end

          hooks[name] = [ in_base, as_hook ]
          invoke_from_option(name, options, &block)
        end
      end
```

```
  # .bundle/ruby/2.2.0/gems/thor-0.19.4/lib/thor/parser/option.rb:113:in `validate!'
    #   parse :foo => true
    #   #=> Option foo with default value true and type boolean
    #
    # The valid types are :boolean, :numeric, :hash, :array and :string. If none
    # is given a default type is assumed. This default type accepts arguments as
    # string (--foo=value) or booleans (just --foo).
    #
    # By default all options are optional, unless :required is given.
    def validate_default_type!
      default_type = case @default
      when nil
        return
      when TrueClass, FalseClass
        required? ? :string : :boolean
      when Numeric
        :numeric
      when Symbol
        :string
      when Hash, Array, String
        @default.class.name.downcase.to_sym
      end

      # TODO: This should raise an ArgumentError in a future version of Thor
      if default_type != @type
        warn "Expected #{@type} default value for '#{switch_name}'; got #{@default.inspect} (#{default_type})"
      end
    end
```
@bf4 bf4 merged commit 7efb362 into rails-api:master Jan 7, 2017
@bf4 bf4 deleted the fix_method_redefined_warning branch January 7, 2017 04:04
bf4 added a commit that referenced this pull request Jan 7, 2017
bf4 added a commit that referenced this pull request Jan 10, 2017
* Merge pull request #1990 from mxie/mx-result-typo

Fix typos and capitalization in Relationship Links docs [ci skip]

* Merge pull request #1992 from ojiry/bump_ruby_versions

Run tests by Ruby 2.2.6 and 2.3.3

* Merge pull request #1994 from bf4/promote_architecture

Promote important architecture description that answers a lot of questions we get
Conflicts:
	docs/ARCHITECTURE.md

* Merge pull request #1999 from bf4/typos

Fix typos [ci skip]

* Merge pull request #2000 from berfarah/patch-1

Link to 0.10.3 tag instead of `master` branch

* Merge pull request #2007 from bf4/check_ci

Test was failing due to change in JSON exception message when parsing empty string

* Swap out KeyTransform for CaseTransform (#1993)

* delete KeyTransform, use CaseTransform

* added changelog

Conflicts:
	CHANGELOG.md

* Merge pull request #2005 from kofronpi/support-ruby-2.4

Update jsonapi runtime dependency to 0.1.1.beta6

* Bump to v0.10.4

* Merge pull request #2018 from rails-api/bump_version

Bump to v0.10.4 [ci skip]
Conflicts:
	CHANGELOG.md

* Merge pull request #2019 from bf4/fix_method_redefined_warning

Fix AMS warnings

* Merge pull request #2020 from bf4/silence_grape_warnings

Silence Grape warnings

* Merge pull request #2017 from bf4/remove_warnings

Fix mt6 assert_nil warnings

* Updated isolated tests to assert correct behavior. (#2010)

* Updated isolated tests to assert correct behavior.
* Added check to get unsafe params if rails version is great than 5

* Merge pull request #2012 from bf4/cleanup_isolated_jsonapi_renderer_tests_a_bit

Cleanup assertions in isolated jsonapi renderer tests a bit

* Add Model#attributes helper; make test attributes explicit

* Fix model attributes accessors

* Fix typos

* Randomize testing of compatibility layer against regressions

* Test bugfix

* Add CHANGELOG

* Merge pull request #1981 from groyoh/link_doc

Fix relationship links doc
Conflicts:
	CHANGELOG.md
GregPK pushed a commit to GregPK/active_model_serializers that referenced this pull request Apr 25, 2017
* Merge pull request rails-api#1990 from mxie/mx-result-typo

Fix typos and capitalization in Relationship Links docs [ci skip]

* Merge pull request rails-api#1992 from ojiry/bump_ruby_versions

Run tests by Ruby 2.2.6 and 2.3.3

* Merge pull request rails-api#1994 from bf4/promote_architecture

Promote important architecture description that answers a lot of questions we get
Conflicts:
	docs/ARCHITECTURE.md

* Merge pull request rails-api#1999 from bf4/typos

Fix typos [ci skip]

* Merge pull request rails-api#2000 from berfarah/patch-1

Link to 0.10.3 tag instead of `master` branch

* Merge pull request rails-api#2007 from bf4/check_ci

Test was failing due to change in JSON exception message when parsing empty string

* Swap out KeyTransform for CaseTransform (rails-api#1993)

* delete KeyTransform, use CaseTransform

* added changelog

Conflicts:
	CHANGELOG.md

* Merge pull request rails-api#2005 from kofronpi/support-ruby-2.4

Update jsonapi runtime dependency to 0.1.1.beta6

* Bump to v0.10.4

* Merge pull request rails-api#2018 from rails-api/bump_version

Bump to v0.10.4 [ci skip]
Conflicts:
	CHANGELOG.md

* Merge pull request rails-api#2019 from bf4/fix_method_redefined_warning

Fix AMS warnings

* Merge pull request rails-api#2020 from bf4/silence_grape_warnings

Silence Grape warnings

* Merge pull request rails-api#2017 from bf4/remove_warnings

Fix mt6 assert_nil warnings

* Updated isolated tests to assert correct behavior. (rails-api#2010)

* Updated isolated tests to assert correct behavior.
* Added check to get unsafe params if rails version is great than 5

* Merge pull request rails-api#2012 from bf4/cleanup_isolated_jsonapi_renderer_tests_a_bit

Cleanup assertions in isolated jsonapi renderer tests a bit

* Add Model#attributes helper; make test attributes explicit

* Fix model attributes accessors

* Fix typos

* Randomize testing of compatibility layer against regressions

* Test bugfix

* Add CHANGELOG

* Merge pull request rails-api#1981 from groyoh/link_doc

Fix relationship links doc
Conflicts:
	CHANGELOG.md
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