Skip to content

Commit

Permalink
Created a yaml export for Roles and VirtualClasses definitions with c…
Browse files Browse the repository at this point in the history
…olumns.
  • Loading branch information
gaspard committed Apr 11, 2011
1 parent 63c18c2 commit bdbad90
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 17 deletions.
13 changes: 8 additions & 5 deletions app/controllers/virtual_classes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class VirtualClassesController < ApplicationController
before_filter :find_virtual_class, :except => [:index, :create, :new, :import]
before_filter :find_virtual_class, :except => [:index, :create, :new, :import, :export]
before_filter :visitor_node
before_filter :check_is_admin
layout :admin_layout
Expand All @@ -16,7 +16,7 @@ def index
@virtual_classes << klass
end
end
else
else
Node.native_classes.each do |kpath, klass|
@virtual_classes << klass
end
Expand All @@ -43,11 +43,14 @@ def index
end

def export
data = secure(::Role) do
::Role.export
res = {}
secure(::Role) do
::Role.all.each do |role|
res[role.name] = role.export
end
end

### TODO
send_data(res.to_yaml, :filename=>"roles.yml", :type => 'text/yaml')
end

def import
Expand Down
7 changes: 7 additions & 0 deletions app/models/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ def index_name
self.index.to_s.gsub(/\A\./,'')
end

def export
{
'ptype' => ptype,
'index' => index,
}
end

protected
def set_defaults
self[:site_id] = current_site.id
Expand Down
26 changes: 26 additions & 0 deletions app/models/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ def defined_safe_columns
@safe_column ||= defined_columns.values.sort {|a,b| a.name <=> b.name}
end

def export
res = {
'name' => name,
'superclass' => superclass.name,
'kpath' => kpath,
'type' => type,
}
if !defined_columns.empty?
res['columns'] = export_columns
end
res
end

private
def set_defaults
self[:type] = self.class.to_s
Expand All @@ -62,4 +75,17 @@ def check_can_save
def expire_vclass_cache
VirtualClass.expire_cache!
end

def export_columns
res = {}

defined_columns.each do |name, column|
col = {'ptype' => column.ptype.to_s}
if column.index then
col['index'] = column.index
end
res[name] = col
end
res
end
end
14 changes: 10 additions & 4 deletions app/models/virtual_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,6 @@ def build_virtual_class(klass, data)
return data[klass]['result'] = virtual_class
end
end

def export
# TODO
end
end

self.caches_by_site ||= {}
Expand Down Expand Up @@ -293,6 +289,16 @@ def to_s
name
end

def export
res = super
%w{idx_class idx_scope idx_reverse_scope}.each do |k|
value = self[k]
next if value.blank?
res[k] = value
end
res
end

def icon=(txt)
self[:icon] = txt.gsub('..', '.') # SECURITY
end
Expand Down
2 changes: 1 addition & 1 deletion lib/zena/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def zen_routes
:member => { :crop_form => :get }

resources :relations
resources :virtual_classes, :collection => {:import => :post}
resources :virtual_classes, :collection => {:import => :post, :export => :get}
resources :columns, :collection => {:import => :post}

resources :sites,
Expand Down
10 changes: 7 additions & 3 deletions lib/zena/use/display.rb
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,15 @@ def r_show(code = nil)
if node.list_context?
@context[:node] = node.move_to("#{node}.first", node.klass.first, :query => node.opts[:query])
return r_show
elsif node.will_be?(String)
return "<%= #{node} %>"
end

return nil unless method = code || get_attribute_or_eval
if method = code || get_attribute_or_eval
# ok
elsif node.will_be?(String) || node.will_be?(Time)
method = RubyLess.translate(self, 'this')
else
return nil
end

klass = method.klass

Expand Down
13 changes: 13 additions & 0 deletions test/functional/virtual_classes_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,19 @@ class VirtualClassesControllerTest < Zena::Controller::TestCase
end
end
end # importing virtual class definitions

context 'exporting virtual class definitions' do
subject do
{:action => :export}
end

should 'dump virtual classes to yaml' do
get_subject
res = YAML.load @response.body
assert_equal %w{Blog Contact Letter Original Post Reference Tag Task Tracker}, res.keys.sort
assert_equal res['Original'], roles(:Original).export
end
end # importing virtual class definitions
end # that is an admin
end # A logged in user
end
5 changes: 5 additions & 0 deletions test/integration/zafu_compiler/display.yml
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ show_with_custom_tlabel_shortcut:
tem: "/anglais/"
res: "<li><label>anglais</label> <span>Etat des travaux</span></li>"

show_date_context:
src: "<b do='created_at' do='show' format='%Y'/><b do='event_at' do='show' format='%Y'/>"
# 'event_at' is a context. If nil ==> do not enter context.
res: "<b>2006</b>"

rendering:
src: "==<r:headers Content-Disposition='attachment; filename=special_#{title}.csv'/>=="
tem: "==<% set_headers(\"Content-Disposition\" => \"attachment; filename=special_#{@node.prop['title']}.csv\") %>=="
Expand Down
20 changes: 18 additions & 2 deletions test/unit/column_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ColumnTest < Zena::Unit::TestCase
end
end
end # with the name of a hardwire property

context 'ending with _ids' do
subject do
Column.create(:role_id => roles_id(:Task), :ptype => 'string', :name => 'secure_on_destroy_ids')
Expand All @@ -84,7 +84,7 @@ class ColumnTest < Zena::Unit::TestCase
end
end
end # with the name of a hardwire property

context 'ending with _id' do
subject do
Column.create(:role_id => roles_id(:Task), :ptype => 'string', :name => 'secure_on_destroy_id')
Expand All @@ -98,4 +98,20 @@ class ColumnTest < Zena::Unit::TestCase
end # with the name of a hardwire property
end # Creating a column

context 'exporting a column' do
setup do
login(:lion)
end

subject do
columns(:Post_date)
end

should 'export attributes' do
assert_equal({
'ptype' => 'datetime',
'index' => '.idx_datetime1',
}, subject.export)
end
end # exporting a role
end
28 changes: 27 additions & 1 deletion test/unit/role_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class RoleTest < Zena::Unit::TestCase
end
end # with an admin visitor
end # Creating a new Role

context 'with a role' do
subject do
roles(:Original)
Expand All @@ -127,5 +127,31 @@ class RoleTest < Zena::Unit::TestCase
assert_equal %w{origin tz weight}, subject.defined_safe_columns.map(&:name)
end
end # with a virtual class


context 'exporting a role' do
setup do
login(:lion)
end

subject do
roles(:Original)
end

should 'export attributes' do
assert_equal({
'superclass' => 'Node',
'type' => 'Role',
'kpath' => 'N',
'columns' => {
'weight' => {'index' => '', 'ptype' => 'integer'},
'tz' => {'index' => '', 'ptype' => 'string'},
'origin' => {'index' => 'string', 'ptype' => 'string'},
},
}, subject.export)
end
end # exporting a role

# Indexed columns in role tested in NodeTest

end
23 changes: 22 additions & 1 deletion test/unit/virtual_class_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ def setup_cache_test
assert_equal %w{reference reference_for set_tag}, subject.map(&:other_role).sort
end
end # on relations

context 'on new_instance' do
subject do
VirtualClass['Letter'].new_instance(
Expand Down Expand Up @@ -878,4 +878,25 @@ def setup_cache_test
end
end # without an existing superclass
end # importing virtual class definitions

context 'exporting a virtual class' do
setup do
login(:lion)
end

subject do
secure(VirtualClass) { VirtualClass['Post'] }
end

should 'export attributes' do
assert_equal({
'superclass' => 'Note',
'type' => 'VirtualClass',
'kpath' => 'NNP',
'columns' => {
'date' => {'name' => 'date', 'index' => '.idx_datetime1', 'ptype' => 'datetime'},
},
}, subject.export)
end
end
end

0 comments on commit bdbad90

Please sign in to comment.