Skip to content

Commit

Permalink
Can specify which fieldsets to use in queries
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidMikeSimon committed Oct 2, 2012
1 parent 0308039 commit af332b7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/model_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def self.included(base)
base.write_inheritable_attribute :mochigome_focus_settings, nil
base.class_inheritable_reader :mochigome_focus_settings

# FIXME: Unclear on how this interacts with inheritance...
# FIXME: Unclear on how this should interact with inheritance...
base.write_inheritable_attribute :mochigome_aggregation_settings_sets, {}
base.class_inheritable_reader :mochigome_aggregation_settings_sets
end
Expand Down
6 changes: 4 additions & 2 deletions lib/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def initialize(layers, options = {})
@name = options.delete(:root_name).try(:to_s) || "report"
access_filter = options.delete(:access_filter) || lambda {|cls| {}}
aggregate_sources = options.delete(:aggregate_sources) || {}
@fieldsets = options.delete(:fieldsets) || {}
unless options.empty?
raise QueryError.new("Unknown options: #{options.keys.inspect}")
end
Expand Down Expand Up @@ -106,17 +107,18 @@ def self.tree_root_to_leaf_paths(t)
def generate_datanodes(model_ids)
model_datanodes = {}
model_ids.keys.each do |model|
model_datanodes[model.name] ||= {}
# TODO: Find a way to do this without loading all recs at one time
model.all(
:conditions => {model.primary_key => model_ids[model].to_a},
:order => model.mochigome_focus_settings.get_ordering
).each_with_index do |rec, seq_idx|
f = rec.mochigome_focus
dn = DataNode.new(f.type_name, f.name)
dn.merge!(f.field_data)
dn.merge!(f.field_data(@fieldsets[model]))
dn[:id] = rec.id
dn[:internal_type] = model.name
(model_datanodes[model.name] ||= {})[rec.id] = [dn, seq_idx]
model_datanodes[model.name][rec.id] = [dn, seq_idx]
end
end
return model_datanodes
Expand Down
2 changes: 1 addition & 1 deletion lib/subgroup_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def name
@rec.value
end

def field_data
def field_data(fieldset_names = nil)
{}
end
end
Expand Down
4 changes: 3 additions & 1 deletion test/app_root/app/models/owner.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class Owner < ActiveRecord::Base
acts_as_mochigome_focus
acts_as_mochigome_focus do |f|
f.fieldset :age, ["birth_date", "age"]
end

has_many :stores

Expand Down
17 changes: 16 additions & 1 deletion test/unit/query_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

@product_e = create(:product, :name => "Product E") # No category

@john = create(:owner, :first_name => "John", :last_name => "Smith")
@john = create(:owner, :first_name => "John", :last_name => "Smith",
:birth_date => 25.years.ago)
@store_x = create(:store, :name => "John's Store", :owner => @john)

@jane = create(:owner, :first_name => "Jane", :last_name => "Doe",
Expand Down Expand Up @@ -680,4 +681,18 @@ def assert_no_children(obj)
assert_equal "Category 1", (dn/0/1).name
refute (dn/0/1).has_key?('Sales count')
end

it "can specify which fieldsets to use for particular models" do
q = Mochigome::Query.new(
[Owner],
:fieldsets => {
Owner => [:default, :age]
}
)
dn = q.run
assert_equal "John Smith", (dn/0).name
assert_equal 25, (dn/0)["age"]
assert_equal "Jane Doe", (dn/1).name
assert_equal 30.years.ago.to_date, (dn/1)["birth_date"]
end
end

0 comments on commit af332b7

Please sign in to comment.