module Lolita::Controllers::RailsHelpers

Protected Instance Methods

alert(msg,options = {}) click to toggle source
# File lib/lolita/controllers/rails_helpers.rb, line 28
def alert(msg,options = {})
  lolita_head_msg(msg,"Lolita-Alert",options)
end
build_resource(attributes=nil) click to toggle source
# File lib/lolita/controllers/rails_helpers.rb, line 59
def build_resource(attributes=nil)
  self.run(:before_build_resource)
  attributes||=resource_attributes
  self.resource=resource_with_attributes(resource_class.new,attributes)
  self.run(:after_build_resource)
end
build_response_for(conf_part,options={}) click to toggle source
# File lib/lolita/controllers/rails_helpers.rb, line 74
def build_response_for(conf_part,options={})
  # FIXME when asked for some resources that always create new object, there may
  # not be any args, like lolita.report on something like that
  @component_options = options
  if nested_list?
    @component_object = nested_resource_class(conf_part.to_sym)
  else
    @component_object = resource_class.lolita.send(conf_part.to_sym)
  end
  @component_builder = @component_object.build(@component_options)
end
error(msg, options = {}) click to toggle source
# File lib/lolita/controllers/rails_helpers.rb, line 32
def error(msg, options = {})
  lolita_head_msg(msg,"Lolita-Error",options)
end
get_resource(id=nil) click to toggle source
# File lib/lolita/controllers/rails_helpers.rb, line 54
def get_resource(id=nil)
  self.resource = resource_class.lolita.dbi.find_by_id(id || params[:id])
  raise Lolita::RecordNotFound unless self.resource
end
lolita_head_msg(msg,key,options ={}) click to toggle source
# File lib/lolita/controllers/rails_helpers.rb, line 36
def lolita_head_msg(msg,key,options ={})
  msg = Base64.encode64(msg).gsub("\n","")
  response.headers[key] = msg
  if options[:return]
    {key => msg}
  end
end
nested_list?() click to toggle source
# File lib/lolita/controllers/rails_helpers.rb, line 66
def nested_list?
  params[:nested] && params[:nested][:path]
end
nested_resource_class(conf_part) click to toggle source
# File lib/lolita/controllers/rails_helpers.rb, line 70
def nested_resource_class conf_part
  @nested_resource_class ||= params[:nested][:parent].constantize.lolita.send(conf_part.to_sym).by_path(params[:nested][:path])
end
notice(msg, options = {}) click to toggle source

Return instance variable named as resource For 'posts' instance variable will be @posts

# File lib/lolita/controllers/rails_helpers.rb, line 24
def notice(msg, options = {})
  lolita_head_msg(msg,"Lolita-Notice",options)
end
resource_attributes() click to toggle source
# File lib/lolita/controllers/rails_helpers.rb, line 44
def resource_attributes
  fix_attributes(params[resource_name] || {})
end
resource_with_attributes(current_resource,attributes={}) click to toggle source
# File lib/lolita/controllers/rails_helpers.rb, line 48
def resource_with_attributes(current_resource,attributes={})
  permitted_attributes = ActionController::Parameters.new(attributes || resource_attributes).permit!
  current_resource.attributes = permitted_attributes
  current_resource
end

Private Instance Methods

fix_attributes(attributes) click to toggle source
# File lib/lolita/controllers/rails_helpers.rb, line 89
def fix_attributes attributes
  fix_rails_date_attributes attributes
end
fix_rails_date_attributes(attributes) click to toggle source
# File lib/lolita/controllers/rails_helpers.rb, line 93
def fix_rails_date_attributes attributes
  #{"created_at(1i)"=>"2011", "created_at(2i)"=>"4", "created_at(3i)"=>"19", "created_at(4i)"=>"16", "created_at(5i)"=>"14"}
  date_attributes = {}
  attributes.each_pair do |k,v|
    if k.to_s =~ /(.+)\((\d)i\)$/
      date_attributes[$1] = {} unless date_attributes[$1]
      date_attributes[$1][$2.to_i] = v.to_i
      attributes.delete(k)
    end
  end
  date_attributes.each_pair do |k,v|
    unless v.detect{|index,value| value == 0 && index<4}
      attributes[k] = v.size == 3 ? Date.new(v[1],v[2],v[3]) : Time.new(v[1],v[2],v[3],v[4],v[5])
    end
  end
  attributes
end
switch_locale() { || ... } click to toggle source
# File lib/lolita/controllers/rails_helpers.rb, line 111
def switch_locale
  begin
    old_locale = ::I18n.locale
    if params[:locale]
      Lolita.locale = params[:locale].to_sym
      session[:lolita_locale] = Lolita.locale
    elsif Lolita.locales.include?(session[:lolita_locale])
      Lolita.locale = session[:lolita_locale]
    end
    ::I18n.locale = Lolita.locale
    yield
  ensure
    ::I18n.locale = old_locale
  end
end