Skip to content

Commit

Permalink
reggae_instance_list for flat structures
Browse files Browse the repository at this point in the history
  • Loading branch information
herbdaily committed Mar 27, 2012
1 parent dc9bbfc commit d45b0a4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 37 deletions.
10 changes: 5 additions & 5 deletions lib/marley/joints/forum.rb
Expand Up @@ -8,18 +8,18 @@ def topics(params=nil)
if params && params[:tags]
filters << {:id => MR::Tag.join(:messages_tags, :tag_id => :id).select(:message_id).filter(:tag => params[:tags])}
end
filters.inject(self.dataset.filter(:parent_id => nil)) {|ds,f| ds.filter(f)}
filters.inject(self.list_dataset(:parent_id => nil)) {|ds,f| ds.filter(f)}
end
def list(params=nil)
(params.is_a?(Sequel::Dataset) ? params : topics(params)).eager(associations).all.map{|t| t.thread}
topics(params).eager_graph(:user).all.map{|t| t.thread}
end
end
module InstanceMethods
def write_cols
super.push(:topic_id, :parent_id)
new? ? super.push(:topic_id, :parent_id) : super
end
def children
self.class.filter(:parent_id => id).eager(associations)
self.class.list_dataset.filter(:parent_id => id)
end
def thread
return reggae_instance if children.all.length==0
Expand Down Expand Up @@ -47,7 +47,7 @@ def section_nav
]
end
def recent_topics
list(:date_created > Date.today - 2)
list(lambda {date_created > Date.today - 2})
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/marley/joints/tags.rb
Expand Up @@ -21,7 +21,7 @@ def initialize(opts={})
tags_meth=tag_class.resource_name.pluralize.to_sym
@class_methods_mod=Module.new do |m|
define_method(:list_dataset) {|*args|
super.eager_graph(tags_meth => proc{|ds| ds.filter(:tags__user_id => tag_class.associations.include?(:user) ? current_user[:id] : nil)})
super.eager_graph({tags_meth => proc{|ds| ds.filter(:tags__user_id => tag_class.associations.include?(:user) ? current_user[:id] : nil)}})
}
end
@instance_methods_mod=Module.new do |m|
Expand Down
54 changes: 26 additions & 28 deletions lib/marley/plugins/orm_rest_convenience.rb
Expand Up @@ -30,7 +30,20 @@ def list_dataset(params={})
dataset.filter(params)
end
def list(params={})
list_dataset.filter(params).all
list_dataset(params).all
end
def reggae_instance_list(params={})
items=list(params)
if items.length==0
Marley::ReggaeMessage.new(:title => 'Nothing Found')
else
cols=items[0].rest_cols
Marley::ReggaeInstanceList.new(
:name => resource_name,
:schema => items[0].schema(true),
:items => items.map{|i| cols.map{|c|i.send(c)}}
)
end
end
def sti
plugin :single_table_inheritance, :"#{self.to_s.sub(/.*::/,'').underscore}_type", :model_map => lambda{|v| MR.const_get(v.to_sym)}, :key_map => lambda{|klass|klass.name.sub(/.*::/,'')}
Expand All @@ -51,44 +64,29 @@ def actions(parent_instance=nil); _instance_actions; end

def rest_associations;[];end

def ro_reggae_schema
def reggae_schema(list_schema=false)
Marley::ReggaeSchema.new(
rest_cols.map do |col_name|
db_spec=db_schema.to_hash[col_name]
col_type=db_spec ? db_spec[:db_type].downcase : "text"
col_type=:password if col_name.to_s.match(/password/)
restrictions=RESTRICT_RO
restrictions|=RESTRICT_HIDE if hidden_cols.include?(col_name)
[col_type, col_name, restrictions,send(col_name)]
end
)
end
def reggae_schema
Marley::ReggaeSchema.new(
rest_cols.map do |col_name|
db_spec=db_schema.to_hash[col_name]
col_type=db_spec ? db_spec[:db_type].downcase : "text"
col_type=:password if col_name.to_s.match(/password/)
restrictions=0
restrictions|=RESTRICT_HIDE if hidden_cols.include?(col_name)
restrictions|=RESTRICT_RO unless write_cols.include?(col_name)
restrictions|=RESTRICT_REQ if required_cols.include?(col_name) || (db_spec && !db_spec[:allow_null])
[col_type, col_name, restrictions,send(col_name)]
if list_schema
restrictions=RESTRICT_RO
restrictions|=RESTRICT_HIDE if hidden_cols.include?(col_name)
[col_type, col_name, restrictions]
else
restrictions=0
restrictions|=RESTRICT_HIDE if hidden_cols.include?(col_name)
restrictions|=RESTRICT_RO unless write_cols.include?(col_name)
restrictions|=RESTRICT_REQ if required_cols.include?(col_name) || (db_spec && !db_spec[:allow_null])
[col_type, col_name, restrictions,send(col_name)]
end
end
)
end
def to_s
respond_to?('name') ? name : "#{self.class.name} #{id.to_s}"
end
def ro_reggae_instance(parent_instance=nil)
a=Marley::ReggaeInstance.new(
{:name => self.class.resource_name,:url => url ,:new_rec => self.new?,:schema => ro_reggae_schema,:actions => self.actions(parent_instance)}
)
a.contents=rest_associations.to_a.map do |assoc|
assoc.map{|instance| instance.ro_reggae_instance(self)}
end unless new?
a
end
def reggae_instance(parent_instance=nil)
a=Marley::ReggaeInstance.new(
{:name => self.class.resource_name,:url => url ,:new_rec => self.new?,:schema => reggae_schema,:actions => self.actions(parent_instance)}
Expand Down
3 changes: 1 addition & 2 deletions lib/marley/reggae.rb
Expand Up @@ -107,8 +107,7 @@ def set_values(col_hash)
end
end
class ReggaeInstanceList < ReggaeResource
properties :name,:description,:actions,:items
#not implemented yet
properties :name,:description,:actions,:group_actions,:items
end
class ReggaeMsg < ReggaeResource
properties :title,:description
Expand Down
2 changes: 1 addition & 1 deletion rdoc/forum_load.rb
Expand Up @@ -5,7 +5,7 @@
#this kinda sucks...
Sequel::Model.send(:alias_method, :save!, :save)

USERS=50
USERS=5
TOPICS=5
TAGS=3
REPLIES=[4,2]
Expand Down

0 comments on commit d45b0a4

Please sign in to comment.