Skip to content

Commit 48d0bb0

Browse files
committed
Migrate over PR RestPack#113
1 parent aec062f commit 48d0bb0

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ restpack_serializer allows you to quickly provide a set of RESTful endpoints for
1515
## Getting Started
1616

1717
### For rails projects:
18-
After adding the gem `restpack_serializer` to your Gemfile, add this code to `config/initializers/restpack_serializer.rb`:
18+
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.
19+
20+
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`:
1921

2022
```ruby
2123
Dir[Rails.root.join('app/serializers/**/*.rb')].each do |path|

lib/restpack_serializer/factory.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def self.create(*identifiers)
99
def self.classify(identifier)
1010
normalised_identifier = identifier.to_s.underscore
1111
[normalised_identifier, normalised_identifier.singularize].each do |format|
12-
klass = RestPack::Serializer.class_map[format]
12+
klass = RestPack::Serializer.Serializer.class_for_identifier(format)
1313
return klass.new if klass
1414
end
1515

lib/restpack_serializer/serializable.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
module RestPack
1313
module Serializer
1414
extend ActiveSupport::Concern
15-
mattr_accessor :class_map
1615
@@class_map ||= {}
1716

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

23+
def self.class_for_identifier(identifier)
24+
klass = begin
25+
"#{identifier}_serializer".camelize.constantize
26+
rescue NameError => e
27+
nil
28+
end
29+
30+
klass ||= @@class_map[identifier]
31+
end
32+
2433
include RestPack::Serializer::Paging
2534
include RestPack::Serializer::Resource
2635
include RestPack::Serializer::Single

0 commit comments

Comments
 (0)