Skip to content

Commit

Permalink
Migrate over PR RestPack#113
Browse files Browse the repository at this point in the history
  • Loading branch information
benfb committed Apr 7, 2020
1 parent aec062f commit 48d0bb0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ restpack_serializer allows you to quickly provide a set of RESTful endpoints for
## Getting Started

### For rails projects:
After adding the gem `restpack_serializer` to your Gemfile, add this code to `config/initializers/restpack_serializer.rb`:
Rails autoloading will automatically pull in top-level serializers. This means you no longer need to restart your server when changing serializers in development. If you previously had a restpack serializer initializer script, please remove it, as the two are not compatible.

If your serializers or your model classes are nested in a module, you'll need to make sure you add this to your project in `config/initializers/restpack_serializer.rb`:

```ruby
Dir[Rails.root.join('app/serializers/**/*.rb')].each do |path|
Expand Down
2 changes: 1 addition & 1 deletion lib/restpack_serializer/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.create(*identifiers)
def self.classify(identifier)
normalised_identifier = identifier.to_s.underscore
[normalised_identifier, normalised_identifier.singularize].each do |format|
klass = RestPack::Serializer.class_map[format]
klass = RestPack::Serializer.Serializer.class_for_identifier(format)
return klass.new if klass
end

Expand Down
11 changes: 10 additions & 1 deletion lib/restpack_serializer/serializable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
module RestPack
module Serializer
extend ActiveSupport::Concern
mattr_accessor :class_map
@@class_map ||= {}

included do
Expand All @@ -21,6 +20,16 @@ module Serializer
@@class_map[identifier.split('/').last] = self
end

def self.class_for_identifier(identifier)
klass = begin
"#{identifier}_serializer".camelize.constantize
rescue NameError => e
nil
end

klass ||= @@class_map[identifier]
end

include RestPack::Serializer::Paging
include RestPack::Serializer::Resource
include RestPack::Serializer::Single
Expand Down

0 comments on commit 48d0bb0

Please sign in to comment.