Sha256: 4a0779e451b44c32be5b175125fbf633b600b18dcfcfe104cbbd1f02399338a7

Contents?: true

Size: 1.23 KB

Versions: 3

Compression:

Stored size: 1.23 KB

Contents

class NetzkeLayout < ActiveRecord::Base
  EXT_UNRELATED_ATTRIBUTES = %w{ id layout_id position created_at updated_at }
  # Multi user support
  def self.user
    @@user ||= nil
  end
  
  def self.user=(user)
    @@user = user
  end
  
  def self.user_id
    user && user.id
  end
  
  # normal create, but with a user_id merged-in
  def self.create_with_user(config)
    create(config.merge(:user_id => user_id))
  end
  
  def items
    items_class.constantize.find_all_by_layout_id(id, :order => 'position')
  end
  
  def self.by_widget(widget_name)
    self.find(:first, :conditions => {:widget_name => widget_name.to_s, :user_id => user_id})
  end

  def move_item(old_index, new_index)
    layout_item = items[old_index]
    layout_item.remove_from_list
    layout_item.insert_at(new_index + 1)
  end

  def items_arry
    unrelated_attrs_eraser = EXT_UNRELATED_ATTRIBUTES.inject({}){|h,el| h.merge(el => nil)} # => {:id => nil, :layout_id => nil, ...}
    items.map(&:attributes).map do |i| 
      # delete unrelated attributes
      i.merge(unrelated_attrs_eraser).convert_keys {|k| k.to_sym}
    end
  end
  
  def items_arry_without_hidden
    items_arry.reject{|i| i[:hidden] && !i[:name] == :id} # 'id' is exceptional, should always be sent
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
netzke-core-0.2.7 lib/app/models/netzke_layout.rb
netzke-core-0.2.6 lib/app/models/netzke_layout.rb
netzke-core-0.2.8 lib/app/models/netzke_layout.rb