Class: RenderSync::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/render_sync/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, scopes = nil) ⇒ Resource

Constructor

model - The ActiveModel instace for this Resource scopes - The optional scopes to prefix polymorphic paths with.

Can be a Symbol/String, a parent model or an RenderSync::Scope
or an Array with any combination.

Examples

class User < ActiveRecord::Base
  sync :all
  sync_scope :cool, -> { where(cool: true) }
  sync_scope :in_group, ->(group) { where(group_id: group.id) }
end

user = User.find(1)

resource = Resource.new(user)
resource.polymorphic_path => "/users/1"
resource.polymorphic_new_path => "/users/new"

resource = Resource.new(user, :admin)
resource.polymorphic_path => "/admin/users/1"
resource.polymorphic_new_path => "/admin/users/new"

resource = Resource.new(user, [:staff, :restricted])
resource.polymorphic_path => "/staff/restricted/users/1"
resource.polymorphic_new_path => "/staff/restricted/users/new"

resource = Resource.new(user, project)
resource.polymorphic_path => "/projects/2/users/1"
resource.polymorphic_new_path => "/projects/2/users/new"

resource = Resource.new(user, User.cool)
resource.polymorphic_path => "/cool/users/2"
resource.polymorphic_new_path => "/cool/users/new"

resource = Resource.new(user, User.in_group(group))
resource.polymorphic_path => "/in_group/group/3/users/2"
resource.polymorphic_new_path => "/in_group/group/3/users/new"

resource = Resource.new(user, [:admin, User.cool, User.in_group(group)])
resource.polymorphic_path => "admin/cool/in_group/group/3/users/2"
resource.polymorphic_new_path => "admin/cool/in_group/group/3/users/new"

resource = Resource.new(user, [:admin, project])
resource.polymorphic_path => "/admin/projects/2/users/1"
resource.polymorphic_new_path => "/admin/projects/2/users/new"


56
57
58
59
# File 'lib/render_sync/resource.rb', line 56

def initialize(model, scopes = nil)
  self.model = model
  self.scopes = scopes
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



5
6
7
# File 'lib/render_sync/resource.rb', line 5

def model
  @model
end

#scopesObject

Returns the value of attribute scopes.



5
6
7
# File 'lib/render_sync/resource.rb', line 5

def scopes
  @scopes
end

Instance Method Details

#base_nameObject



74
75
76
# File 'lib/render_sync/resource.rb', line 74

def base_name
  name.split('/').last
end

#idObject



66
67
68
# File 'lib/render_sync/resource.rb', line 66

def id
  model.id
end

#model_pathObject

Returns an unscoped Pathname for the model (e.g. /users/1)



100
101
102
# File 'lib/render_sync/resource.rb', line 100

def model_path
  Pathname.new('/').join(plural_name, id.to_s)
end

#nameObject



70
71
72
# File 'lib/render_sync/resource.rb', line 70

def name
  model.class.model_name.to_s.underscore
end

#plural_nameObject



78
79
80
# File 'lib/render_sync/resource.rb', line 78

def plural_name
  name.pluralize
end

#polymorphic_new_pathObject

Returns the scoped Pathname for a new model (e.g. /users/1/todos/new)



110
111
112
# File 'lib/render_sync/resource.rb', line 110

def polymorphic_new_path
  scopes_path.join(plural_name, "new")
end

#polymorphic_pathObject

Returns the scoped Pathname for the model (e.g. /users/1/todos/2)



105
106
107
# File 'lib/render_sync/resource.rb', line 105

def polymorphic_path
  scopes_path.join(plural_name, id.to_s)
end

#scopes_pathObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/render_sync/resource.rb', line 82

def scopes_path
  path = Pathname.new('/')
  unless scopes.nil?
    paths = scopes.map do |scope|
      if scope.is_a?(RenderSync::Scope)
        scope.polymorphic_path.relative_path_from(path)
      elsif scope.class.respond_to? :model_name
        Resource.new(scope).polymorphic_path.relative_path_from(path)
      else
        scope.to_s
      end
    end
    path = path.join(*paths)
  end
  path
end