Commit Graph

609 Commits

Author SHA1 Message Date
Benjamin Fleischer
34d55e4729 Remove extra reflection classes 2017-04-23 17:46:55 -05:00
Benjamin Fleischer
7d8fb1606b Cleanup 2017-04-23 17:42:30 -05:00
Benjamin Fleischer
ee69293c8f Refactor reflection building serializer class 2017-04-23 15:21:29 -05:00
Benjamin Fleischer
079b3d6841 Refactor collection reflection 2017-04-23 14:53:45 -05:00
Benjamin Fleischer
1bddd9fdb5 Refactor 2017-04-23 14:47:55 -05:00
Benjamin Fleischer
fad4ef1046 Refactor reflection building of association 2017-04-23 14:19:04 -05:00
Benjamin Fleischer
cb16457bb3 Make reflection explicitly dependents on association 2017-04-23 14:18:30 -05:00
Benjamin Fleischer
ba2aa1fdfd Remove dead comments 2017-04-23 14:18:09 -05:00
Benjamin Fleischer
43c3c231ef Use reflection key since we have it 2017-04-23 14:17:59 -05:00
Benjamin Fleischer
3ba4a8c9b2 Always return an enumerator 2017-04-23 14:17:06 -05:00
Benjamin Fleischer
6cd6ed7e78 Move association serialization to association 2017-04-22 22:13:52 -05:00
Benjamin Fleischer
c2dccbac5f Move attributes cache method out of concern 2017-04-22 22:13:52 -05:00
Benjamin Fleischer
f76ea3d39e Merge pull request #2089 from bf4/improve_reflection_interface
Improve reflection internal interface
2017-04-22 21:55:34 -05:00
Mike Kelly
b2f5f32036 Reword ActiveModelSerializer::Model docs for clarity
Fixed some typos, and reworked a sentence to be clearer.
2017-03-28 09:44:53 -04:00
Benjamin Fleischer
f327b6be0c Improve reflection internal interface 2017-03-27 22:46:07 -05:00
Benjamin Fleischer
80e470dcdb Merge pull request #2074 from bf4/make_serializer_interface_more_obvious
Make serializer interface more obvious
2017-03-18 22:41:02 -05:00
Benjamin Fleischer
2e71bc47f4 Improve comments; move caching concern to caching.rb 2017-03-16 10:14:18 -05:00
Benjamin Fleischer
36b4eac79b Make serializer interface more obvious 2017-03-14 20:54:54 -05:00
Benjamin Fleischer
47e82e09b1 Make behavior explicit 2017-03-12 20:18:48 -05:00
Benjamin Fleischer
9c26ffe2d6 Better variables; allow looking serializer from class 2017-03-12 20:16:54 -05:00
Benjamin Fleischer
a36b25d2db Add rubocop binstub that rspects file patterns
Best of both worlds!

(Because you can't override the default rubocop includes)

The binstub basically, lets me safely `rubocop test/foo_test.rb`
instead of `bundle exec rubocop test/foo_test.rb`

```bash
  # ~/.profile
  # https://twitter.com/tpope/status/165631968996900865
  # tl;dr `mkdir .git/safe` to add `bin` to path, e.g. `bin/rails`
  PATH=".git/safe/../../bin:$PATH"
```
2017-03-12 15:50:05 -05:00
Benjamin Fleischer
28b8e3dd17 Bump to v0.10.5 2017-03-07 15:41:20 -06:00
Benjamin Fleischer
81a9fbd1c3 Merge pull request #2046 from leonelgalan/bug_filter_parameters
Fixes bug in Test::Schema when using filter_parameters
2017-02-16 11:55:23 -06:00
Benjamin Fleischer
006956e56b ActiveModel::Model handles the ActiveModel::Errors API
As pointed out in
https://github.com/rails-api/active_model_serializers/issues/2049

ActiveModel::Model already extends ActiveModel::Translation which
implements human_attribute_name and lookup_ancestors
2017-02-15 20:30:28 -06:00
Leonel Galan
7b9d71e99b Fixes bug in Test::Schema when using filter_parameters 2017-02-06 14:58:36 -05:00
Benjamin Fleischer
28c1b5bef6 Document Model delcared attributes 2017-01-31 09:02:51 -06:00
Benjamin Fleischer
93ca27fe44 Merge 0-10-stable into master (to fix breaking change). (#2023)
* 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
2017-01-10 02:28:50 -06:00
Benjamin Fleischer
ced317d827 Fix thor warning to set type: :boolean, was setting { banner: "" }
```
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
```
2017-01-06 17:16:57 -06:00
Benjamin Fleischer
655c721d0d Bump to v0.10.4
Conflicts:
	CHANGELOG.md
2017-01-06 16:23:54 -06:00
Benjamin Fleischer
05430fb233 Fix typos
```
go get -u github.com/client9/misspell/cmd/misspell
misspell  -w -q -error -source=text {app,config,lib,test}/**/*
```

>   workers   = flag.Int("j", 0, "Number of workers, 0 = number of CPUs")
>   writeit   = flag.Bool("w", false, "Overwrite file with corrections (default is just to display)")
>   quietFlag = flag.Bool("q", false, "Do not emit misspelling output")
>   outFlag   = flag.String("o", "stdout", "output file or [stderr|stdout|]")
>   format    = flag.String("f", "", "'csv', 'sqlite3' or custom Golang template for output")
>   ignores   = flag.String("i", "", "ignore the following corrections, comma separated")
>   locale    = flag.String("locale", "", "Correct spellings using locale perferances for US or UK.  Default is to use a neutral variety of English.  Setting locale to US will correct the British spelling of 'colour' to 'color'")
>   mode      = flag.String("source", "auto", "Source mode: auto=guess, go=golang source, text=plain or markdown-like text")
>   debugFlag = flag.Bool("debug", false, "Debug matching, very slow")
>   exitError = flag.Bool("error", false, "Exit with 2 if misspelling found")
2016-12-11 23:41:23 -06:00
L. Preston Sego III
0422a1e772 Swap out KeyTransform for CaseTransform (#1993)
* delete KeyTransform, use CaseTransform

* added changelog
2016-12-07 10:49:38 -05:00
Benjamin Fleischer
80af763d2e Make test attributes explicit
- Organize test poros with associations and by serializer
- Freeze derived attributes/associations against mutation
- Cleanup PORO fixtures
2016-12-04 19:33:39 -06:00
Benjamin Fleischer
772b7999c5 Better AMS Model attributes interface 2016-11-21 09:14:26 -06:00
Benjamin Fleischer
cd09e89006 Bump to 0.10.3 2016-11-21 08:55:22 -06:00
L. Preston Sego III
d31d741f43 Make serializer lookup configurable (#1757) 2016-11-16 18:38:40 +01:00
Yohan Robert
d0de53cbb2 Fix namespace lookup for collections and has_many (#1973) 2016-11-15 08:35:58 -05:00
L. Preston Sego III
b29395b0ac This adds namespace lookup to serializer_for (#1968)
* This adds namespace lookup to serializer_for

* address rubocop issue

* address @bf4's feedback

* add docs

* update docs, add more tests

* apparently rails master doesn't have before filter

* try to address serializer cache issue between tests

* update cache for serializer lookup to include namespace in the key, and fix the tests for explicit namespace

* update docs, and use better cache key creation method

* update docs [ci skip]

* update docs [ci skip]

* add to changelog [ci skip]
2016-11-09 07:57:39 -05:00
Benjamin Fleischer
6c6e45b23f Replace fail/rescue CollectionSerializer::NoSerializerError with throw/catch :no_serializer (#1767) 2016-09-26 10:18:27 -04:00
Lee Richmond
2145540795 Add include_data :if_sideloaded (#1931)
For JSONAPI, `include_data` currently means, "should we populate the
'data'" key for this relationship. Current options are true/false.

This adds the `:if_sideloaded` option. This means "only
populate the 'data' key when we are sideloading this relationship." This
is because 'data' is often only relevant to sideloading, and causes a
database hit.

Addresses https://github.com/rails-api/active_model_serializers/issues/1555
2016-09-25 12:57:19 -04:00
Lee Richmond
a77dfdaa85 Ensure valid jsonapi when blank relationship (#1930)
If you specify include_data false, and do not have any links for this
relationship, we would output something like:

`{ relationships: { comments: {} } }`

This is not valid jsonapi. We will now render

`{ relationships: { comments: { meta: {} } } }`

Instead.

Relevant jsonapi spec: http://jsonapi.org/format/#document-resource-object-relationships
2016-09-19 14:10:50 -04:00
Lee Richmond
11bd778f17 Merge pull request #1912 from richmolj/generator
Ensure generator picks up ApplicationSerializer
2016-09-06 12:42:37 -07:00
Lee Richmond
19b5abf66e Disable pagination links via config 2016-09-06 15:06:54 -04:00
Lee Richmond
3f16b75a68 Ensure generator picks up ApplicationSerializer
ApplicationSerializer could exist, but not be loaded. So, we check
existence by looking at the filesystem instead of defined?.

Fixes https://github.com/rails-api/active_model_serializers/issues/1890
2016-09-05 19:00:25 -04:00
Benjamin Fleischer
d34069b6ae Merge pull request #1799 from ScottKbka/patch-1
Update rendering.md
2016-08-31 16:18:47 -05:00
Benjamin Fleischer
20e394d512 Refactor Association into Field like everything else (#1897)
* Make assocations asserts easier to understand

* Refactor Association into Field like everything else

* Make assocation serializer/links/meta lazier

* Push association deeper into relationship

* Simplify association usage in relationships

* Better naming of reflection parent serializer

* Easier to read association method
2016-08-31 09:35:41 -04:00
L. Preston Sego III
7d2997b3ff Organize Serializer concerns into concerns/ (#1898) 2016-08-31 06:53:51 -04:00
Benjamin Fleischer
010243484e Merge pull request #1887 from johnnymo87/correct-comment
Correct comment
2016-08-31 01:04:36 -05:00
Benjamin Fleischer
7c4ce7bcb0 Organize Serializer concerns into concerns/ 2016-08-31 00:59:48 -05:00
Ian C. Anderson
2423ca4999 Support key transformation for Attributes adapter (#1889)
The `:attributes` adapter is the default one, but it did not support
key transformation. This was very surprising behavior, since the
"Configuration Options" page in the guides didn't mention that this
behavior was not supported by the attributes adapter.

This commit adds key transform support to the attributes adapter, and
adds documentation about the default transform for the attributes
adapter (which is `:unaltered`).

This commit also handles arrays when transforming keys, which was needed
in the case where you're serializing a collection with the Attributes
adapter. With the JSON adapter, it was always guaranteed to pass a hash
to the KeyTransform functions because of the top-level key. Since there
is no top-level key for the Attributes adapter, the return value could
be an array.
2016-08-25 15:21:27 -04:00
johnnymo87
f96d97d3e9 Correct comment 2016-08-22 13:10:29 +00:00