Class: Mochigome::SubgroupModel

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

Overview

An instance of SubgroupModel acts like a class that derives from AR::Base, but is used to do subgrouping in Query and does not interact with the database itself.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, attr) ⇒ SubgroupModel

Returns a new instance of SubgroupModel.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/subgroup_model.rb', line 8

def initialize(model, attr)
  @model = model
  @attr = attr
  s = @model.mochigome_focus_settings
  if s && s.options[:custom_subgroup_exprs][attr]
    @attr_expr = s.options[:custom_subgroup_exprs][attr]
  elsif @model.columns_hash[@attr.to_s].try(:type) == :boolean
    @attr_expr = Mochigome::sql_bool_to_string(
      model.arel_table[@attr], "#{human_name.titleize}: "
    ).call(model.arel_table)
  else
    @attr_expr = nil
  end
  if @attr_expr && @attr_expr.respond_to?(:expr)
    @attr_expr = @attr_expr.expr
  end
  @focus_settings = Mochigome::ReportFocusSettings.new(@model)
  @focus_settings.type_name "#{@model.human_name} #{@attr.to_s.humanize}"
  @focus_settings.name lambda{|r| r.send(attr)}
end

Instance Attribute Details

#attrObject (readonly)

Returns the value of attribute attr.



6
7
8
# File 'lib/subgroup_model.rb', line 6

def attr
  @attr
end

#modelObject (readonly)

Returns the value of attribute model.



6
7
8
# File 'lib/subgroup_model.rb', line 6

def model
  @model
end

Instance Method Details

#acts_as_mochigome_focus?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/subgroup_model.rb', line 71

def acts_as_mochigome_focus?
  true
end

#all(options = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/subgroup_model.rb', line 75

def all(options = {})
  c = options[:conditions]
  unless c.is_a?(Hash) && c.size == 1 && c[@attr].is_a?(Array)
    raise QueryError.new("Invalid conditions given to SubgroupModel#all")
  end
  recs = c[@attr].compact.map do |val|
    SubgroupPseudoRecord.new(self, val)
  end
  # TODO: Support some kind of custom ordering
  recs.sort!{|a,b| a.value <=> b.value}
  recs
end

#arel_primary_keyObject



55
56
57
58
59
60
61
# File 'lib/subgroup_model.rb', line 55

def arel_primary_key
  if @attr_expr
    @attr_expr
  else
    arel_table[@attr]
  end
end

#arel_tableObject



47
48
49
# File 'lib/subgroup_model.rb', line 47

def arel_table
  @model.arel_table
end

#connectionObject



63
64
65
# File 'lib/subgroup_model.rb', line 63

def connection
  @model.connection
end

#human_nameObject



34
35
36
37
# File 'lib/subgroup_model.rb', line 34

def human_name
  # Get rid of duplicate words (i.e. School$school_type)
  "#{@model.human_name} #{@attr.to_s.humanize}".split.map(&:downcase).uniq.join(" ")
end

#mochigome_focus_settingsObject



67
68
69
# File 'lib/subgroup_model.rb', line 67

def mochigome_focus_settings
  @focus_settings
end

#nameObject



29
30
31
32
# File 'lib/subgroup_model.rb', line 29

def name
  # This works as both a valid SQL field name and a valid XML tag name
  "#{@model}__#{@attr}"
end

#primary_keyObject



51
52
53
# File 'lib/subgroup_model.rb', line 51

def primary_key
  @attr
end

#real_model?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/subgroup_model.rb', line 39

def real_model?
  false
end

#to_real_modelObject



43
44
45
# File 'lib/subgroup_model.rb', line 43

def to_real_model
  @model
end