Module: ArIdeyabox

Defined in:
lib/ar_ideyabox.rb

Instance Method Summary collapse

Instance Method Details

#by_positionObject



11
12
13
# File 'lib/ar_ideyabox.rb', line 11

def by_position
  order(:position)
end

#first_el_tryer(object, by) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/ar_ideyabox.rb', line 19

def first_el_tryer(object, by)
  if by.class == Hash
    object.try(by.keys[0]).try(by.values[0])
  else
    object.try(by)
  end
end

#has_not_this(model_name, *args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ar_ideyabox.rb', line 27

def has_not_this(model_name, *args)
  options = args.extract_options!
  join_model = options[:through].to_s.singularize.camelize
  self_id_sym = "#{self.to_s.underscore}_id".to_sym
  array_first_el = options[:by] ? options[:by] : :title
  group_by = options[:group_by]
  includes = options[:includes]
  model_name_id = "#{model_name}_id"

  define_singleton_method("has_not_this_#{model_name}") do |ids|
    excluded_ids = join_model.constantize.where(model_name_id.to_sym => ids).collect(&self_id_sym)
    output = self.where{id << excluded_ids}
    output = output.includes(includes) if includes
    output = output.order(options[:order]) if options[:order]
    if group_by
      output = output.group_by{|a| first_el_tryer(a, group_by)} 
      output.delete(nil)
      output.each do |key, value|
        array = []
        value.each do |el|
          array << [first_el_tryer(el, array_first_el), el.id]
        end
        output[key] = array
      end
      # output.to_a.sort
    else
      output.collect{|p| [first_el_tryer(p, array_first_el), p.id]}
    end
  end
end

#visibleObject

In model just add this line has_not_this :person, through: :professions_people, order: :title, by: :title, group_by: :title :order and :by - optionally :group_by is for group selectors, if you want grouping roles by play titles, just add group_by: :title



7
8
9
# File 'lib/ar_ideyabox.rb', line 7

def visible
  where(visible: true)
end

#visible_by_positionObject



15
16
17
# File 'lib/ar_ideyabox.rb', line 15

def visible_by_position
  visible.by_position
end