Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.class_for_identifier(format)
return klass.new if klass
end

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

@@class_map ||= {}

included do
Expand All @@ -22,6 +22,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