class ViewCell

Public Class Methods

before(&block) click to toggle source

can be called as a block or a method block do … def block; super; …

Calls superclass method
# File lib/view-cell/class.rb, line 52
def before &block
  define_method :before do
    super() if self.class != ViewCell
    instance_exec &block
  end
end
cell(parent, *args) click to toggle source

cell @users

cell @user

cell.user.render @user

cell(:user, user: @user).render

# File lib/view-cell/class.rb, line 23
def cell parent, *args
  if args.first
    # covert to list of objects
    unless [String, Symbol, Array].include?(args[0].class)
      args[0] = [args.first]
    end

    out =
    if args.first.class == Array
      # cell @users
      args.first.map do |object|
        name = object.class.to_s.underscore.to_sym
        ViewCell.get(parent, name).render object
      end.join('')
    else
      # cell(:user, user: @user).profile
      ViewCell.get parent, *args
    end

    out.respond_to?(:html_safe) ? out.html_safe : out
  else
    # cell.user.profile
    ViewCell::Proxy.new(parent)
  end
end
delegate(*list) click to toggle source

delegate current scope methods to parent binding delegate :image_tag, :request, params

# File lib/view-cell/class.rb, line 13
def delegate *list
  list.each do |el|
    define_method(el) { |*args, &block| parent.send(el, *args, &block) }
  end
end
get(parent, name, vars={}) click to toggle source

load cell based on a name, pass context and optional vars ViewCell.get(:user, self) -> UserCell.new(self)

# File lib/view-cell/class.rb, line 5
def get parent, name, vars={}
  ('%sCell' % name.to_s.classify)
    .constantize
    .new parent, vars
end
new(parent=nil, vars={}) click to toggle source
# File lib/view-cell/instance.rb, line 6
def initialize parent=nil, vars={}
  @_parent = parent
  vars.each { |k,v| instance_variable_set "@#{k}", v}
  before
end
template_root(name=nil) click to toggle source

set or get template root directory

# File lib/view-cell/class.rb, line 60
def template_root name=nil
  if name
    self.class.instance_variable_set :@template_root, name
  else
    self.class.instance_variable_get :@template_root
  end
end

Public Instance Methods

before() click to toggle source

called every time for every method in a class

# File lib/view-cell/instance.rb, line 13
def before
end
cell(*args) click to toggle source

call

# File lib/view-cell/instance.rb, line 26
def cell *args
  ViewCell.cell @_parent, *args
end
parent(&block) click to toggle source

access parent scope

# File lib/view-cell/instance.rb, line 17
def parent &block
  if block
    @_parent.instance_exec self, &block
  else
    @_parent
  end
end
template(name) click to toggle source

render template by name

# File lib/view-cell/instance.rb, line 31
def template name
  template_root = self.class.template_root
  class_part    = self.class.to_s.underscore.sub(/_cell$/, '')

  if template_root
    name = [template_root, name].join '/'
  elsif name.is_a?(Symbol)
    name = './app/views/cells/%s/%s' % [class_part, name]
  elsif name.to_s =~ /^\w/
    name = './app/views/cells/%s' % name
  end

  name = name % class_part if name.include?('%s')

  RENDER_CACHE.delete(name) if _development?

  RENDER_CACHE[name] ||= proc do
    # find extension if one not provided
    file_name = name

    unless name =~ /\.\w{2,4}$/
      if (file = Dir['%s*' % name].first)
        file_name = file
      end
    end

    unless File.exist?(file_name)
      raise ArgumentError, 'Template "%s.*" not found' % name
    end

    Tilt.new(file_name)
  end.call

  out = RENDER_CACHE[name].render(self)
  out.respond_to?(:html_safe) ? out.html_safe : out
end

Private Instance Methods

_development?() click to toggle source
# File lib/view-cell/instance.rb, line 70
def _development?
  ENV['RAILS_ENV'] == 'development' || ENV['RACK_ENV'] == 'development'
end