Skip to content

Commit

Permalink
Add SiteNodeField to CoreFields. Added strict? to HashModel, when
Browse files Browse the repository at this point in the history
working with a hash model and using strict it will raise an exception if
method is not found. Added page_url_options to SiteNode for storing the
node_path instead of the id. added setup hash model in Content::Field.
  • Loading branch information
Doug Youch committed Mar 25, 2010
1 parent 4505068 commit 9beb6ff
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
29 changes: 28 additions & 1 deletion app/models/content/core_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ def self.content_fields_handler_info #:nodoc:
{ :name => :header,
:description => 'Header',
:representation => :none,
}
},
{ :name => :site_node,
:description => 'Page URL',
:representation => :string
}
]


Expand Down Expand Up @@ -887,6 +891,29 @@ def assign(entry,values)
end
end

class SiteNodeField < Content::Field #:nodoc:all
field_options :required
setup_model :required do |cls,fld|
cls.has_options fld.model_field.field.to_sym, fld.available_options.clone
end
content_display :text

def active_table_header
ActiveTable::OptionHeader.new(@model_field.field, :label => @model_field.name, :options =>self.available_options)
end

def available_options(atr={})
SiteNode.page_url_options
end


def form_field(f,field_name,field_opts,options={})
f.select field_name, [['--Select--',nil]] + available_options , field_opts.merge(options)
end

filter_setup
end

def self.dynamic_current_value(entry,fld,state = {}) #:nodoc:
Time.now
end
Expand Down
18 changes: 17 additions & 1 deletion app/models/content/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,26 @@ def self.setup_model(*options,&block)
end
end
block.call(cls,self) if block
setup_hash_model(cls)
end
end


def setup_hash_model(cls) #:nodoc:
return unless cls.is_a?(HashModel)

case content_field[:representation]
when :integer
cls.integer_options fld.field.to_sym
when :boolean
cls.boolean_options fld.field.to_sym
end
end

# Returns field information hash from register_content_fields
def content_field
@content_field ||= ContentModel.content_field(self.field_module,self.field_type)
end

@@content_display_methods = {
:text => "Content::Field.text_value(entry.send(@model_field.field),size,options)",
:html => "entry.send(@model_field.field)"
Expand Down
10 changes: 8 additions & 2 deletions app/models/site_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,20 @@ def modifiers(before_only=false,before_idx=nil)

# Returns a select-friendly list of pages with urls (group nodes not included)
# optionally including the root node as well
def self.page_options(include_root = false)
def self.page_options(include_root = false, opts={})
node_type = include_root ? 'node_type IN("P","R","M","J")' : 'node_type IN ("P","M","J")'
SiteNode.find(:all,:conditions => node_type,
:order => 'lft').collect do |page|
[ page.node_type != 'R' ? page.node_path : include_root ,page.id ]
[ page.node_type != 'R' ? page.node_path : include_root , opts[:url] ? page.node_path : page.id ]
end
end

# Returns a select-friendly list of pages with urls (group nodes not included)
# optionally including the root node as well
def self.page_url_options(include_root = false)
self.page_options(include_root, :url => true)
end

# Returns a select-friendly list of pages and groups
# optionally including the root node as well
def self.page_and_group_options(include_root = false)
Expand Down
4 changes: 4 additions & 0 deletions lib/hash_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,15 @@ def method_missing(arg, *args)
#
elsif arg[-1..-1] == "="
raise "Undeclared HashModel variable: #{arg[0..-2]}"
elsif self.strict?
raise "Missing hash model method #{arg}" unless self.instance_variables.include?("@#{arg}")
self.instance_variable_get "@#{arg}"
else
self.instance_variable_get "@#{arg}"
end
end

def strict?; false; end

def valid?
format_data
Expand Down

0 comments on commit 9beb6ff

Please sign in to comment.