Sha256: 8c4381e80d89ad26f278cb989284989efd268e91150525d5cb78254979ade29b

Contents?: true

Size: 1.68 KB

Versions: 3

Compression:

Stored size: 1.68 KB

Contents

module Roar
  module Rails
    # Makes Rails URL helpers work in representers. Dependent on Rails.application.
    module RepresenterMethods
      extend ActiveSupport::Concern
      
      included do |base|
        base.class_eval do
          attr_accessor :_controller
          delegate :request, :env, :to => :_controller
          
          include ActionController::UrlFor
          include ::Rails.application.routes.url_helpers
          
          extend Conventions
        end
      end
      
      module ClassMethods
        # TODO: test?
        def for_model_with_controller(represented, controller)
          # DISCUSS: use #for_model_attributes for overriding?
          from_attributes(compute_attributes_for(represented)) do |rep| 
            rep.represented = represented
            rep._controller = controller 
          end
        end
        
        # TODO: test?
        def serialize_model_with_controller(represented, controller)
          for_model_with_controller(represented, controller).serialize
        end
      end
      
      # Introduces strongly opinionated convenience methods in Representer.
      module Conventions
        def representation_name
          super.to_s.singularize
        end
        
        def collection(name, options={})
          namespace     = self.name.split("::")[-2] # FIXME: this assumption is pretty opinionated.
          singular_name = name.to_s.singularize
          
          super name, options.reverse_merge(
            :as => "representer/#{namespace}/#{singular_name}".classify.constantize,
            #:tag => singular_name  # FIXME: how/where to decide if singular TAG or not?
            )
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
roar-0.8.3 lib/roar/rails/representer_methods.rb
roar-0.8.2 lib/roar/rails/representer_methods.rb
roar-0.8.1 lib/roar/rails/representer_methods.rb