gfdnavi 1.3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/ChangeLog +10949 -0
- data/LICENCE.txt +57 -0
- data/Makefile +18 -0
- data/TODO +132 -0
- data/app/controllers/analysis_controller.rb +700 -0
- data/app/controllers/application.rb +41 -0
- data/app/controllers/draw_method_controller.rb +172 -0
- data/app/controllers/explorer_controller.rb +197 -0
- data/app/controllers/finder_controller.rb +331 -0
- data/app/controllers/function_controller.rb +204 -0
- data/app/controllers/group_controller.rb +115 -0
- data/app/controllers/keyword_attributes_controller.rb +52 -0
- data/app/controllers/knowledge_controller.rb +928 -0
- data/app/controllers/nodes_controller.rb +515 -0
- data/app/controllers/search_controller.rb +341 -0
- data/app/controllers/user_controller.rb +337 -0
- data/app/helpers/analysis_helper.rb +2 -0
- data/app/helpers/application_helper.rb +105 -0
- data/app/helpers/description_helper.rb +2 -0
- data/app/helpers/function_helper.rb +2 -0
- data/app/helpers/group_helper.rb +2 -0
- data/app/helpers/search_helper.rb +3 -0
- data/app/helpers/user_helper.rb +2 -0
- data/app/helpers/work_flow_helper.rb +92 -0
- data/app/models/actual_file.rb +3 -0
- data/app/models/analysis.rb +715 -0
- data/app/models/analysis_history.rb +42 -0
- data/app/models/cross_array.rb +22 -0
- data/app/models/cross_result.rb +254 -0
- data/app/models/derived_spatial_attribute.rb +3 -0
- data/app/models/diagram_cache.rb +16 -0
- data/app/models/diagram_cache_datum.rb +4 -0
- data/app/models/diagram_cache_session.rb +8 -0
- data/app/models/directory.rb +21 -0
- data/app/models/draw_method.rb +48 -0
- data/app/models/draw_method_attribute.rb +8 -0
- data/app/models/draw_parameter.rb +12 -0
- data/app/models/explorer_query.rb +354 -0
- data/app/models/explorer_result.rb +312 -0
- data/app/models/function.rb +75 -0
- data/app/models/function_argument.rb +6 -0
- data/app/models/function_output.rb +4 -0
- data/app/models/group.rb +153 -0
- data/app/models/image.rb +20 -0
- data/app/models/keyword_attribute.rb +179 -0
- data/app/models/knowledge.rb +94 -0
- data/app/models/knowledge_backup.rb +32 -0
- data/app/models/knowledge_figure.rb +16 -0
- data/app/models/knowledge_figure_backup.rb +16 -0
- data/app/models/knowledge_parser.rb +392 -0
- data/app/models/node.rb +605 -0
- data/app/models/node_entity_abstract.rb +112 -0
- data/app/models/node_lineage.rb +6 -0
- data/app/models/node_relation.rb +4 -0
- data/app/models/path_node.rb +60 -0
- data/app/models/query.rb +193 -0
- data/app/models/query_history.rb +12 -0
- data/app/models/results.rb +48 -0
- data/app/models/sign_up_user.rb +25 -0
- data/app/models/spatial_and_time_attribute.rb +11 -0
- data/app/models/user.rb +115 -0
- data/app/models/user_mailer.rb +48 -0
- data/app/models/value_type.rb +3 -0
- data/app/models/variable.rb +70 -0
- data/app/models/virtual_node.rb +457 -0
- data/app/views/analysis/__help_analysis_tab.en.html +57 -0
- data/app/views/analysis/__help_draw_share.en.html +12 -0
- data/app/views/analysis/__help_draw_tab.en.html +6 -0
- data/app/views/analysis/_analysis_function_settings.rhtml +61 -0
- data/app/views/analysis/_analysis_settings.rhtml +28 -0
- data/app/views/analysis/_diagram.rhtml +70 -0
- data/app/views/analysis/_dimension_option.rhtml +59 -0
- data/app/views/analysis/_draw_general_settings.rhtml +217 -0
- data/app/views/analysis/_draw_settings.rhtml +73 -0
- data/app/views/analysis/_history.rhtml +19 -0
- data/app/views/analysis/_popular_diagrams.rhtml +42 -0
- data/app/views/analysis/_save_form.rhtml +44 -0
- data/app/views/analysis/_variables.rhtml +66 -0
- data/app/views/analysis/_variables_order.rhtml +56 -0
- data/app/views/analysis/index.rhtml +195 -0
- data/app/views/analysis/preview.rhtml +3 -0
- data/app/views/analysis/save.rhtml +73 -0
- data/app/views/analysis/show_diagram.rhtml +42 -0
- data/app/views/cross/_display_map.rhtml +201 -0
- data/app/views/cross/_result.rhtml +205 -0
- data/app/views/cross/_show_result_tree.rhtml +40 -0
- data/app/views/cross/_show_results.rhtml +80 -0
- data/app/views/cross/_style.rhtml +21 -0
- data/app/views/cross/index.rhtml +271 -0
- data/app/views/draw_method/_edit_form.rhtml +110 -0
- data/app/views/draw_method/create.rhtml +8 -0
- data/app/views/draw_method/details.rhtml +113 -0
- data/app/views/draw_method/edit.rhtml +8 -0
- data/app/views/draw_method/list.rhtml +80 -0
- data/app/views/explorer/_display_map.rhtml +199 -0
- data/app/views/explorer/_show_result_tree.rhtml +40 -0
- data/app/views/explorer/_show_results.rhtml +101 -0
- data/app/views/explorer/_style.rhtml +21 -0
- data/app/views/explorer/index.rhtml +214 -0
- data/app/views/finder/_children.html.erb +84 -0
- data/app/views/finder/_children_details.html.erb +74 -0
- data/app/views/finder/_children_details_tr.html.erb +196 -0
- data/app/views/finder/create_tree.html.erb +15 -0
- data/app/views/finder/flex.html.erb +52 -0
- data/app/views/finder/index.html.erb +47 -0
- data/app/views/finder/show_details.html.erb +3 -0
- data/app/views/finder/show_images.html.erb +3 -0
- data/app/views/function/_edit_form.rhtml +106 -0
- data/app/views/function/create.rhtml +8 -0
- data/app/views/function/details.rhtml +96 -0
- data/app/views/function/edit.rhtml +8 -0
- data/app/views/function/list.rhtml +80 -0
- data/app/views/group/add_member.rhtml +13 -0
- data/app/views/group/change_owner.rhtml +15 -0
- data/app/views/group/create.rhtml +11 -0
- data/app/views/group/delete_member.rhtml +14 -0
- data/app/views/group/list.rhtml +63 -0
- data/app/views/knowledge/_backup.rhtml +135 -0
- data/app/views/knowledge/_category.rhtml +58 -0
- data/app/views/knowledge/_category_list.rhtml +34 -0
- data/app/views/knowledge/_category_table.rhtml +23 -0
- data/app/views/knowledge/_comment.rhtml +64 -0
- data/app/views/knowledge/_comment_input_form.rhtml +19 -0
- data/app/views/knowledge/_comments.rhtml +84 -0
- data/app/views/knowledge/_form.rhtml +139 -0
- data/app/views/knowledge/_group_figure_form.rhtml +187 -0
- data/app/views/knowledge/_layout_figure.rhtml +61 -0
- data/app/views/knowledge/_layout_figures_in_a_row_above_text.rhtml +42 -0
- data/app/views/knowledge/_layout_figures_under_text.rhtml +45 -0
- data/app/views/knowledge/_layout_one_figure_above_text.rhtml +28 -0
- data/app/views/knowledge/_summary.rhtml +21 -0
- data/app/views/knowledge/edit.rhtml +37 -0
- data/app/views/knowledge/list.rhtml +287 -0
- data/app/views/knowledge/new.rhtml +23 -0
- data/app/views/knowledge/new_from_analysis.rhtml +21 -0
- data/app/views/knowledge/show.rhtml +196 -0
- data/app/views/layouts/gfdnavi.rhtml +105 -0
- data/app/views/nodes/_ancestors.html.erb +89 -0
- data/app/views/nodes/_description_methods_table.html.erb +75 -0
- data/app/views/nodes/_mode.html.erb +16 -0
- data/app/views/nodes/description.html.erb +181 -0
- data/app/views/nodes/description_methods.html.erb +1 -0
- data/app/views/nodes/edit.html.erb +73 -0
- data/app/views/nodes/index.html.erb +25 -0
- data/app/views/nodes/show.html.erb +20 -0
- data/app/views/search/_children_details_mapsearch.rhtml +68 -0
- data/app/views/search/_children_details_tr.rhtml +205 -0
- data/app/views/search/_show_grouplist.rhtml +74 -0
- data/app/views/search/_show_results.rhtml +43 -0
- data/app/views/search/_show_results_global.rhtml +36 -0
- data/app/views/search/_show_results_partial.rhtml +39 -0
- data/app/views/search/_show_results_partial_groupbyfile.rhtml +50 -0
- data/app/views/search/_show_variablelist.rhtml +28 -0
- data/app/views/search/mapsearch.rhtml +176 -0
- data/app/views/search/remapsearch.rjs +18 -0
- data/app/views/search/show_details.rhtml +3 -0
- data/app/views/user/add_user.rhtml +30 -0
- data/app/views/user/change_password.rhtml +25 -0
- data/app/views/user/edit.html.erb +50 -0
- data/app/views/user/index.rhtml +162 -0
- data/app/views/user/login.rhtml +41 -0
- data/app/views/user/logout.rhtml +4 -0
- data/app/views/user/signup.rhtml +36 -0
- data/app/views/user/signup_succeeded.rhtml +36 -0
- data/app/views/user_mailer/authorization_inform.rhtml +15 -0
- data/app/views/user_mailer/signup_accepted.rhtml +12 -0
- data/app/views/user_mailer/signup_confirm.rhtml +14 -0
- data/app/views/user_mailer/signup_inform.rhtml +13 -0
- data/app/views/user_mailer/signup_rejected.rhtml +11 -0
- data/app/views/work_flow/_push_wsdls.rhtml +55 -0
- data/app/views/work_flow/index.rhtml +46 -0
- data/config/database.yml.example +35 -0
- data/config/environment.rb +68 -0
- data/config/gfdnavi.yml.example +95 -0
- data/config/load_config.rb +151 -0
- data/config/routes.rb +35 -0
- data/consistency_checker.rb +129 -0
- data/data/gfdnavi_docs/how_to_draw_multiple_images.knlge +68 -0
- data/data/gfdnavi_docs/how_to_knowledge.knlge +138 -0
- data/data/gfdnavi_docs/how_to_write_document_about_Gfdnavi.knlge +30 -0
- data/data/samples/jmadata.SIGEN +8 -0
- data/data/samples/jmadata.yml +6 -0
- data/data/samples/jmadata/MSM-P/2006.nc.yml +2 -0
- data/data/samples/jmadata/MSM-P/2006.yml +17 -0
- data/data/samples/jmadata/MSM-P/2006/0101.nc +0 -0
- data/data/samples/jmadata/MSM-P/2006/0101.nc.yml +13 -0
- data/data/samples/jmadata/MSM-P/2006/0102.nc +0 -0
- data/data/samples/jmadata/MSM-P/2006/0103.nc +0 -0
- data/data/samples/jmadata/MSM-P/2006/0104.nc +0 -0
- data/data/samples/jmadata/MSM-P/2006/0105.nc +0 -0
- data/data/samples/jmadata/MSM-P/2006/010[1-3].nc.yml +2 -0
- data/data/samples/jmadata/MSM-P/2006/skip_data.rb +96 -0
- data/data/samples/jmadata/MSM-P/2006/skip_data.sh +6 -0
- data/data/samples/reanalysis.SIGEN +8 -0
- data/data/samples/reanalysis/era40.SIGEN +8 -0
- data/data/samples/reanalysis/era40.yml +2 -0
- data/data/samples/reanalysis/era40/t.jan.nc +0 -0
- data/data/samples/reanalysis/era40/t.jan.nc.yml +1 -0
- data/data/samples/reanalysis/ncep.SIGEN +9 -0
- data/data/samples/reanalysis/ncep/T.jan.100hPa.png +0 -0
- data/data/samples/reanalysis/ncep/T.jan.100hPa.png.yml +43 -0
- data/data/samples/reanalysis/ncep/T.jan.nc +0 -0
- data/data/samples/reanalysis/ncep/T.jan.zonal_mean.nc +0 -0
- data/data/samples/reanalysis/ncep/T.jan.zonal_mean.nc.yml +8 -0
- data/data/samples/reanalysis/ncep/T.jan_only_root.nc +0 -0
- data/data/samples/reanalysis/ncep/T.jan_only_root.nc.yml +4 -0
- data/data/samples/reanalysis/ncep/UV.jan.nc +0 -0
- data/data/samples/reanalysis/ncep/UV.jan.nc.yml +10 -0
- data/data/samples/reanalysis/ncep/UV.jan.nc.yml.ja +11 -0
- data/data/samples/sonde_operational.yml +5 -0
- data/data/samples/sonde_operational/std_plev.yml +12 -0
- data/data/samples/sonde_operational/std_plev/41256.spl.nc +0 -0
- data/data/samples/sonde_operational/std_plev/42410.spl.nc +0 -0
- data/data/samples/sonde_operational/std_plev/43333.spl.nc +0 -0
- data/data/samples/sonde_operational/std_plev/43346.spl.nc +0 -0
- data/data/samples/sonde_operational/std_plev/48698.spl.nc +0 -0
- data/data/samples/sonde_operational/std_plev/61641.spl.nc +0 -0
- data/data/samples/sonde_operational/std_plev/63741.spl.nc +0 -0
- data/data/samples/sonde_operational/std_plev/67083.spl.nc +0 -0
- data/data/samples/sonde_operational/std_plev/72261.spl.nc +0 -0
- data/data/samples/sonde_operational/std_plev/76805.spl.nc +0 -0
- data/data/samples/sonde_operational/std_plev/82193.spl.nc +0 -0
- data/data/samples/sonde_operational/std_plev/91165.spl.nc +0 -0
- data/data/samples/sonde_operational/std_plev/91610.spl.nc +0 -0
- data/data/samples/sonde_operational/std_plev/91948.spl.nc +0 -0
- data/data/samples/sonde_operational/std_plev/94294.spl.nc +0 -0
- data/db/NetCDFparser.rb +406 -0
- data/db/create_derived_spatial_attributes.sql +12 -0
- data/db/draw_methods/line.yml +6 -0
- data/db/draw_methods/scatter.yml +38 -0
- data/db/draw_methods/tone.yml +64 -0
- data/db/draw_methods/vector.yml +19 -0
- data/db/dump_db.rb +43 -0
- data/db/functions/additions.yml +10 -0
- data/db/functions/cut.yml +10 -0
- data/db/functions/division.yml +10 -0
- data/db/functions/mean.yml +28 -0
- data/db/functions/multiplication.yml +10 -0
- data/db/functions/stddev.yml +28 -0
- data/db/functions/subtraction.yml +10 -0
- data/db/migrate/001_initial_scheme.rb +289 -0
- data/db/migrate/002_add_open_id_authentication_tables.rb +20 -0
- data/db/register_directory_tree.rb +760 -0
- data/db/restore_db.rb +60 -0
- data/gfdnavi-install +828 -0
- data/install.rb +828 -0
- data/lib/activerecord_gfdnavi.rb +31 -0
- data/lib/basic_authorization.rb +50 -0
- data/lib/create_activeresources.rb +108 -0
- data/lib/execute_analysis.rb +472 -0
- data/lib/file_gfdnavi.rb +38 -0
- data/lib/gphys_gfdnavi.rb +81 -0
- data/lib/localdir.rb +110 -0
- data/lib/login_system.rb +110 -0
- data/lib/multibytes.rb +20 -0
- data/lib/narray_yaml.rb +58 -0
- data/lib/no_rdb_base.rb +40 -0
- data/lib/opendapdir.rb +171 -0
- data/lib/tar.rb +127 -0
- data/lib/tasks/clean.rake +65 -0
- data/lib/tasks/setup.rake +316 -0
- data/lib/tasks/update.rake +62 -0
- data/lib/vizshot_gfdnavi.rb +224 -0
- data/public/crossdomain.xml +4 -0
- data/public/flex/NodeTreeDescriptor.as +55 -0
- data/public/flex/TreeNode.as +253 -0
- data/public/flex/finder.mxml +304 -0
- data/public/flex/finder.swf +0 -0
- data/public/flex/gfdnavi.mxml +52 -0
- data/public/flex/gfdnavi.swf +0 -0
- data/public/images/Help_S-shots/dir_tree_with_add.jpg +0 -0
- data/public/images/analyze.gif +0 -0
- data/public/images/circle.gif +0 -0
- data/public/images/create_from_analysis_button.png +0 -0
- data/public/images/delete.png +0 -0
- data/public/images/down.png +0 -0
- data/public/images/drawbutton.gif +0 -0
- data/public/images/gfdnavi_home_logo.png +0 -0
- data/public/images/gfdnavi_logo_big.png +0 -0
- data/public/images/gfdnavi_logo_med.png +0 -0
- data/public/images/gfdnavi_logo_mini.png +0 -0
- data/public/images/gfdnavi_logo_nobg_big.png +0 -0
- data/public/images/gfdnavi_logo_nobg_med.png +0 -0
- data/public/images/gfdnavi_logo_nobg_small.png +0 -0
- data/public/images/gfdnavi_logo_small.png +0 -0
- data/public/images/gfdnavi_logo_xsmall.png +0 -0
- data/public/images/hbar.png +0 -0
- data/public/images/helpmark16.png +0 -0
- data/public/images/helpmark18.png +0 -0
- data/public/images/kikyu_g.gif +0 -0
- data/public/images/kikyu_r.gif +0 -0
- data/public/images/map_icon_g.gif +0 -0
- data/public/images/map_icon_r.gif +0 -0
- data/public/images/new_knowledge.png +0 -0
- data/public/images/openid.png +0 -0
- data/public/images/read_in_the_form.png +0 -0
- data/public/images/slider_f.gif +0 -0
- data/public/images/slider_f_red.gif +0 -0
- data/public/images/star1.gif +0 -0
- data/public/images/star2.gif +0 -0
- data/public/images/startanim.gif +0 -0
- data/public/images/stopanim.gif +0 -0
- data/public/images/tree/DL.png +0 -0
- data/public/images/tree/anal_viz.png +0 -0
- data/public/images/tree/analyze_visualize_checked_items.png +0 -0
- data/public/images/tree/binary.png +0 -0
- data/public/images/tree/details.png +0 -0
- data/public/images/tree/download_checked_items.png +0 -0
- data/public/images/tree/folder.png +0 -0
- data/public/images/tree/folder_close.gif +0 -0
- data/public/images/tree/image2.png +0 -0
- data/public/images/tree/knowledge.png +0 -0
- data/public/images/tree/knowledges.png +0 -0
- data/public/images/tree/last.gif +0 -0
- data/public/images/tree/last.png +0 -0
- data/public/images/tree/minus.gif +0 -0
- data/public/images/tree/minus_simple.gif +0 -0
- data/public/images/tree/none.gif +0 -0
- data/public/images/tree/none.png +0 -0
- data/public/images/tree/plus.gif +0 -0
- data/public/images/tree/plus_simple.gif +0 -0
- data/public/images/tree/show.png +0 -0
- data/public/images/tree/show_checked_items.png +0 -0
- data/public/images/tree/t.gif +0 -0
- data/public/images/tree/t.png +0 -0
- data/public/images/tree/tate.gif +0 -0
- data/public/images/tree/tate.png +0 -0
- data/public/images/up.png +0 -0
- data/public/images/vbar.png +0 -0
- data/public/images/white.bmp +0 -0
- data/public/images/x.gif +0 -0
- data/public/index.html +100 -0
- data/public/javascripts/AC_OETags.js +269 -0
- data/public/javascripts/analysis.js +751 -0
- data/public/javascripts/application.js +29 -0
- data/public/javascripts/directory_tree.js +102 -0
- data/public/javascripts/draw_method.js +83 -0
- data/public/javascripts/excanvas.js +704 -0
- data/public/javascripts/function.js +107 -0
- data/public/javascripts/gfdnavi.js +175 -0
- data/public/javascripts/knowledge.js +384 -0
- data/public/javascripts/mapsearch.js +313 -0
- data/public/javascripts/progress.js +42 -0
- data/public/javascripts/resizable_box.js +164 -0
- data/public/javascripts/slider.js +260 -0
- data/public/javascripts/tab.js +17 -0
- data/public/javascripts/work_flow.js +452 -0
- data/public/stylesheets/analysis.css +111 -0
- data/public/stylesheets/description.css +71 -0
- data/public/stylesheets/gfdnavi.css +141 -0
- data/public/stylesheets/knowledge.css +218 -0
- data/public/stylesheets/search.css +184 -0
- data/public/stylesheets/tree.css +45 -0
- data/public/stylesheets/tree_for_ie.css +3 -0
- data/public/stylesheets/work_flow.css +78 -0
- data/script/cleaner +11 -0
- data/script/server_ssl +131 -0
- data/test/fixtures/diagram_cache_data.yml +5 -0
- data/test/fixtures/diagram_cache_sessions.yml +6 -0
- data/test/fixtures/diagram_caches.yml +6 -0
- data/test/fixtures/directories.yml +57 -0
- data/test/fixtures/draw_parameters.yml +6 -0
- data/test/fixtures/function_arguments.yml +15 -0
- data/test/fixtures/function_outputs.yml +15 -0
- data/test/fixtures/functions.yml +29 -0
- data/test/fixtures/group_members.yml +33 -0
- data/test/fixtures/groups.yml +25 -0
- data/test/fixtures/images.yml +12 -0
- data/test/fixtures/keyword_attributes.yml +73 -0
- data/test/fixtures/knowledges.yml +10 -0
- data/test/fixtures/node_relations.yml +11 -0
- data/test/fixtures/nodes.yml +298 -0
- data/test/fixtures/sign_up_users.yml +8 -0
- data/test/fixtures/spatial_and_time_attributes.yml +29 -0
- data/test/fixtures/user_mailer/authorization_inform +11 -0
- data/test/fixtures/user_mailer/signup_accepted +10 -0
- data/test/fixtures/user_mailer/signup_confirm +17 -0
- data/test/fixtures/user_mailer/signup_inform +10 -0
- data/test/fixtures/user_mailer/signup_rejected +9 -0
- data/test/fixtures/users.yml +31 -0
- data/test/fixtures/value_types.yml +10 -0
- data/test/fixtures/variables.yml +17 -0
- data/test/functional/analysis_controller_test.rb +169 -0
- data/test/functional/description_controller_test.rb +18 -0
- data/test/functional/function_controller_test.rb +18 -0
- data/test/functional/group_controller_test.rb +18 -0
- data/test/functional/search_controller_test.rb +83 -0
- data/test/functional/user_controller_test.rb +77 -0
- data/test/functional/webservice_analysis_api_test.rb +12 -0
- data/test/functional/work_flow_controller_test.rb +18 -0
- data/test/test_helper.rb +28 -0
- data/test/unit/diagram_cache_datum_test.rb +10 -0
- data/test/unit/diagram_cache_session_test.rb +10 -0
- data/test/unit/diagram_cache_test.rb +10 -0
- data/test/unit/directory_test.rb +49 -0
- data/test/unit/draw_parameter_test.rb +10 -0
- data/test/unit/function_argument_test.rb +10 -0
- data/test/unit/function_output_test.rb +10 -0
- data/test/unit/function_test.rb +10 -0
- data/test/unit/group_test.rb +48 -0
- data/test/unit/image_test.rb +13 -0
- data/test/unit/keyword_attribute_test.rb +33 -0
- data/test/unit/knowledge_test.rb +13 -0
- data/test/unit/node_relation_test.rb +20 -0
- data/test/unit/node_test.rb +85 -0
- data/test/unit/sign_up_user_test.rb +20 -0
- data/test/unit/spatial_and_time_attribute_test.rb +29 -0
- data/test/unit/user_mailer_test.rb +101 -0
- data/test/unit/user_test.rb +134 -0
- data/test/unit/value_type_test.rb +10 -0
- data/test/unit/variable_test.rb +35 -0
- data/vendor/plugins/acts_as_list/README +23 -0
- data/vendor/plugins/acts_as_list/init.rb +3 -0
- data/vendor/plugins/acts_as_list/lib/active_record/acts/list.rb +256 -0
- data/vendor/plugins/acts_as_list/test/list_test.rb +332 -0
- data/vendor/plugins/acts_as_tree/README +26 -0
- data/vendor/plugins/acts_as_tree/Rakefile +22 -0
- data/vendor/plugins/acts_as_tree/init.rb +1 -0
- data/vendor/plugins/acts_as_tree/lib/active_record/acts/tree.rb +96 -0
- data/vendor/plugins/acts_as_tree/test/abstract_unit.rb +0 -0
- data/vendor/plugins/acts_as_tree/test/acts_as_tree_test.rb +219 -0
- data/vendor/plugins/acts_as_tree/test/database.yml +0 -0
- data/vendor/plugins/acts_as_tree/test/fixtures/mixin.rb +0 -0
- data/vendor/plugins/acts_as_tree/test/fixtures/mixins.yml +0 -0
- data/vendor/plugins/acts_as_tree/test/schema.rb +0 -0
- data/vendor/plugins/will_paginate/CHANGELOG +92 -0
- data/vendor/plugins/will_paginate/LICENSE +18 -0
- data/vendor/plugins/will_paginate/README.rdoc +131 -0
- data/vendor/plugins/will_paginate/Rakefile +62 -0
- data/vendor/plugins/will_paginate/examples/apple-circle.gif +0 -0
- data/vendor/plugins/will_paginate/examples/index.haml +69 -0
- data/vendor/plugins/will_paginate/examples/index.html +92 -0
- data/vendor/plugins/will_paginate/examples/pagination.css +90 -0
- data/vendor/plugins/will_paginate/examples/pagination.sass +91 -0
- data/vendor/plugins/will_paginate/init.rb +1 -0
- data/vendor/plugins/will_paginate/lib/will_paginate.rb +86 -0
- data/vendor/plugins/will_paginate/lib/will_paginate/array.rb +16 -0
- data/vendor/plugins/will_paginate/lib/will_paginate/collection.rb +145 -0
- data/vendor/plugins/will_paginate/lib/will_paginate/core_ext.rb +32 -0
- data/vendor/plugins/will_paginate/lib/will_paginate/finder.rb +247 -0
- data/vendor/plugins/will_paginate/lib/will_paginate/named_scope.rb +132 -0
- data/vendor/plugins/will_paginate/lib/will_paginate/named_scope_patch.rb +39 -0
- data/vendor/plugins/will_paginate/lib/will_paginate/version.rb +9 -0
- data/vendor/plugins/will_paginate/lib/will_paginate/view_helpers.rb +373 -0
- data/vendor/plugins/will_paginate/will_paginate.gemspec +21 -0
- metadata +586 -0
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
require 'test/unit'
|
|
2
|
+
|
|
3
|
+
require 'rubygems'
|
|
4
|
+
gem 'activerecord', '>= 1.15.4.7794'
|
|
5
|
+
require 'active_record'
|
|
6
|
+
|
|
7
|
+
require "#{File.dirname(__FILE__)}/../init"
|
|
8
|
+
|
|
9
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
|
|
10
|
+
|
|
11
|
+
def setup_db
|
|
12
|
+
ActiveRecord::Schema.define(:version => 1) do
|
|
13
|
+
create_table :mixins do |t|
|
|
14
|
+
t.column :pos, :integer
|
|
15
|
+
t.column :parent_id, :integer
|
|
16
|
+
t.column :created_at, :datetime
|
|
17
|
+
t.column :updated_at, :datetime
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def teardown_db
|
|
23
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
|
24
|
+
ActiveRecord::Base.connection.drop_table(table)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
class Mixin < ActiveRecord::Base
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
class ListMixin < Mixin
|
|
32
|
+
acts_as_list :column => "pos", :scope => :parent
|
|
33
|
+
|
|
34
|
+
def self.table_name() "mixins" end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
class ListMixinSub1 < ListMixin
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
class ListMixinSub2 < ListMixin
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
class ListWithStringScopeMixin < ActiveRecord::Base
|
|
44
|
+
acts_as_list :column => "pos", :scope => 'parent_id = #{parent_id}'
|
|
45
|
+
|
|
46
|
+
def self.table_name() "mixins" end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class ListTest < Test::Unit::TestCase
|
|
51
|
+
|
|
52
|
+
def setup
|
|
53
|
+
setup_db
|
|
54
|
+
(1..4).each { |counter| ListMixin.create! :pos => counter, :parent_id => 5 }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def teardown
|
|
58
|
+
teardown_db
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def test_reordering
|
|
62
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
|
63
|
+
|
|
64
|
+
ListMixin.find(2).move_lower
|
|
65
|
+
assert_equal [1, 3, 2, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
|
66
|
+
|
|
67
|
+
ListMixin.find(2).move_higher
|
|
68
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
|
69
|
+
|
|
70
|
+
ListMixin.find(1).move_to_bottom
|
|
71
|
+
assert_equal [2, 3, 4, 1], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
|
72
|
+
|
|
73
|
+
ListMixin.find(1).move_to_top
|
|
74
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
|
75
|
+
|
|
76
|
+
ListMixin.find(2).move_to_bottom
|
|
77
|
+
assert_equal [1, 3, 4, 2], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
|
78
|
+
|
|
79
|
+
ListMixin.find(4).move_to_top
|
|
80
|
+
assert_equal [4, 1, 3, 2], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def test_move_to_bottom_with_next_to_last_item
|
|
84
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
|
85
|
+
ListMixin.find(3).move_to_bottom
|
|
86
|
+
assert_equal [1, 2, 4, 3], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def test_next_prev
|
|
90
|
+
assert_equal ListMixin.find(2), ListMixin.find(1).lower_item
|
|
91
|
+
assert_nil ListMixin.find(1).higher_item
|
|
92
|
+
assert_equal ListMixin.find(3), ListMixin.find(4).higher_item
|
|
93
|
+
assert_nil ListMixin.find(4).lower_item
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def test_injection
|
|
97
|
+
item = ListMixin.new(:parent_id => 1)
|
|
98
|
+
assert_equal "parent_id = 1", item.scope_condition
|
|
99
|
+
assert_equal "pos", item.position_column
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def test_insert
|
|
103
|
+
new = ListMixin.create(:parent_id => 20)
|
|
104
|
+
assert_equal 1, new.pos
|
|
105
|
+
assert new.first?
|
|
106
|
+
assert new.last?
|
|
107
|
+
|
|
108
|
+
new = ListMixin.create(:parent_id => 20)
|
|
109
|
+
assert_equal 2, new.pos
|
|
110
|
+
assert !new.first?
|
|
111
|
+
assert new.last?
|
|
112
|
+
|
|
113
|
+
new = ListMixin.create(:parent_id => 20)
|
|
114
|
+
assert_equal 3, new.pos
|
|
115
|
+
assert !new.first?
|
|
116
|
+
assert new.last?
|
|
117
|
+
|
|
118
|
+
new = ListMixin.create(:parent_id => 0)
|
|
119
|
+
assert_equal 1, new.pos
|
|
120
|
+
assert new.first?
|
|
121
|
+
assert new.last?
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def test_insert_at
|
|
125
|
+
new = ListMixin.create(:parent_id => 20)
|
|
126
|
+
assert_equal 1, new.pos
|
|
127
|
+
|
|
128
|
+
new = ListMixin.create(:parent_id => 20)
|
|
129
|
+
assert_equal 2, new.pos
|
|
130
|
+
|
|
131
|
+
new = ListMixin.create(:parent_id => 20)
|
|
132
|
+
assert_equal 3, new.pos
|
|
133
|
+
|
|
134
|
+
new4 = ListMixin.create(:parent_id => 20)
|
|
135
|
+
assert_equal 4, new4.pos
|
|
136
|
+
|
|
137
|
+
new4.insert_at(3)
|
|
138
|
+
assert_equal 3, new4.pos
|
|
139
|
+
|
|
140
|
+
new.reload
|
|
141
|
+
assert_equal 4, new.pos
|
|
142
|
+
|
|
143
|
+
new.insert_at(2)
|
|
144
|
+
assert_equal 2, new.pos
|
|
145
|
+
|
|
146
|
+
new4.reload
|
|
147
|
+
assert_equal 4, new4.pos
|
|
148
|
+
|
|
149
|
+
new5 = ListMixin.create(:parent_id => 20)
|
|
150
|
+
assert_equal 5, new5.pos
|
|
151
|
+
|
|
152
|
+
new5.insert_at(1)
|
|
153
|
+
assert_equal 1, new5.pos
|
|
154
|
+
|
|
155
|
+
new4.reload
|
|
156
|
+
assert_equal 5, new4.pos
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def test_delete_middle
|
|
160
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
|
161
|
+
|
|
162
|
+
ListMixin.find(2).destroy
|
|
163
|
+
|
|
164
|
+
assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
|
165
|
+
|
|
166
|
+
assert_equal 1, ListMixin.find(1).pos
|
|
167
|
+
assert_equal 2, ListMixin.find(3).pos
|
|
168
|
+
assert_equal 3, ListMixin.find(4).pos
|
|
169
|
+
|
|
170
|
+
ListMixin.find(1).destroy
|
|
171
|
+
|
|
172
|
+
assert_equal [3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
|
173
|
+
|
|
174
|
+
assert_equal 1, ListMixin.find(3).pos
|
|
175
|
+
assert_equal 2, ListMixin.find(4).pos
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def test_with_string_based_scope
|
|
179
|
+
new = ListWithStringScopeMixin.create(:parent_id => 500)
|
|
180
|
+
assert_equal 1, new.pos
|
|
181
|
+
assert new.first?
|
|
182
|
+
assert new.last?
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def test_nil_scope
|
|
186
|
+
new1, new2, new3 = ListMixin.create, ListMixin.create, ListMixin.create
|
|
187
|
+
new2.move_higher
|
|
188
|
+
assert_equal [new2, new1, new3], ListMixin.find(:all, :conditions => 'parent_id IS NULL', :order => 'pos')
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def test_remove_from_list_should_then_fail_in_list?
|
|
193
|
+
assert_equal true, ListMixin.find(1).in_list?
|
|
194
|
+
ListMixin.find(1).remove_from_list
|
|
195
|
+
assert_equal false, ListMixin.find(1).in_list?
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def test_remove_from_list_should_set_position_to_nil
|
|
199
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
|
200
|
+
|
|
201
|
+
ListMixin.find(2).remove_from_list
|
|
202
|
+
|
|
203
|
+
assert_equal [2, 1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
|
204
|
+
|
|
205
|
+
assert_equal 1, ListMixin.find(1).pos
|
|
206
|
+
assert_equal nil, ListMixin.find(2).pos
|
|
207
|
+
assert_equal 2, ListMixin.find(3).pos
|
|
208
|
+
assert_equal 3, ListMixin.find(4).pos
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def test_remove_before_destroy_does_not_shift_lower_items_twice
|
|
212
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
|
213
|
+
|
|
214
|
+
ListMixin.find(2).remove_from_list
|
|
215
|
+
ListMixin.find(2).destroy
|
|
216
|
+
|
|
217
|
+
assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5', :order => 'pos').map(&:id)
|
|
218
|
+
|
|
219
|
+
assert_equal 1, ListMixin.find(1).pos
|
|
220
|
+
assert_equal 2, ListMixin.find(3).pos
|
|
221
|
+
assert_equal 3, ListMixin.find(4).pos
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
class ListSubTest < Test::Unit::TestCase
|
|
227
|
+
|
|
228
|
+
def setup
|
|
229
|
+
setup_db
|
|
230
|
+
(1..4).each { |i| ((i % 2 == 1) ? ListMixinSub1 : ListMixinSub2).create! :pos => i, :parent_id => 5000 }
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def teardown
|
|
234
|
+
teardown_db
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def test_reordering
|
|
238
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
|
239
|
+
|
|
240
|
+
ListMixin.find(2).move_lower
|
|
241
|
+
assert_equal [1, 3, 2, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
|
242
|
+
|
|
243
|
+
ListMixin.find(2).move_higher
|
|
244
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
|
245
|
+
|
|
246
|
+
ListMixin.find(1).move_to_bottom
|
|
247
|
+
assert_equal [2, 3, 4, 1], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
|
248
|
+
|
|
249
|
+
ListMixin.find(1).move_to_top
|
|
250
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
|
251
|
+
|
|
252
|
+
ListMixin.find(2).move_to_bottom
|
|
253
|
+
assert_equal [1, 3, 4, 2], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
|
254
|
+
|
|
255
|
+
ListMixin.find(4).move_to_top
|
|
256
|
+
assert_equal [4, 1, 3, 2], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
def test_move_to_bottom_with_next_to_last_item
|
|
260
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
|
261
|
+
ListMixin.find(3).move_to_bottom
|
|
262
|
+
assert_equal [1, 2, 4, 3], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
def test_next_prev
|
|
266
|
+
assert_equal ListMixin.find(2), ListMixin.find(1).lower_item
|
|
267
|
+
assert_nil ListMixin.find(1).higher_item
|
|
268
|
+
assert_equal ListMixin.find(3), ListMixin.find(4).higher_item
|
|
269
|
+
assert_nil ListMixin.find(4).lower_item
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
def test_injection
|
|
273
|
+
item = ListMixin.new("parent_id"=>1)
|
|
274
|
+
assert_equal "parent_id = 1", item.scope_condition
|
|
275
|
+
assert_equal "pos", item.position_column
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
def test_insert_at
|
|
279
|
+
new = ListMixin.create("parent_id" => 20)
|
|
280
|
+
assert_equal 1, new.pos
|
|
281
|
+
|
|
282
|
+
new = ListMixinSub1.create("parent_id" => 20)
|
|
283
|
+
assert_equal 2, new.pos
|
|
284
|
+
|
|
285
|
+
new = ListMixinSub2.create("parent_id" => 20)
|
|
286
|
+
assert_equal 3, new.pos
|
|
287
|
+
|
|
288
|
+
new4 = ListMixin.create("parent_id" => 20)
|
|
289
|
+
assert_equal 4, new4.pos
|
|
290
|
+
|
|
291
|
+
new4.insert_at(3)
|
|
292
|
+
assert_equal 3, new4.pos
|
|
293
|
+
|
|
294
|
+
new.reload
|
|
295
|
+
assert_equal 4, new.pos
|
|
296
|
+
|
|
297
|
+
new.insert_at(2)
|
|
298
|
+
assert_equal 2, new.pos
|
|
299
|
+
|
|
300
|
+
new4.reload
|
|
301
|
+
assert_equal 4, new4.pos
|
|
302
|
+
|
|
303
|
+
new5 = ListMixinSub1.create("parent_id" => 20)
|
|
304
|
+
assert_equal 5, new5.pos
|
|
305
|
+
|
|
306
|
+
new5.insert_at(1)
|
|
307
|
+
assert_equal 1, new5.pos
|
|
308
|
+
|
|
309
|
+
new4.reload
|
|
310
|
+
assert_equal 5, new4.pos
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
def test_delete_middle
|
|
314
|
+
assert_equal [1, 2, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
|
315
|
+
|
|
316
|
+
ListMixin.find(2).destroy
|
|
317
|
+
|
|
318
|
+
assert_equal [1, 3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
|
319
|
+
|
|
320
|
+
assert_equal 1, ListMixin.find(1).pos
|
|
321
|
+
assert_equal 2, ListMixin.find(3).pos
|
|
322
|
+
assert_equal 3, ListMixin.find(4).pos
|
|
323
|
+
|
|
324
|
+
ListMixin.find(1).destroy
|
|
325
|
+
|
|
326
|
+
assert_equal [3, 4], ListMixin.find(:all, :conditions => 'parent_id = 5000', :order => 'pos').map(&:id)
|
|
327
|
+
|
|
328
|
+
assert_equal 1, ListMixin.find(3).pos
|
|
329
|
+
assert_equal 2, ListMixin.find(4).pos
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
acts_as_tree
|
|
2
|
+
============
|
|
3
|
+
|
|
4
|
+
Specify this +acts_as+ extension if you want to model a tree structure by providing a parent association and a children
|
|
5
|
+
association. This requires that you have a foreign key column, which by default is called +parent_id+.
|
|
6
|
+
|
|
7
|
+
class Category < ActiveRecord::Base
|
|
8
|
+
acts_as_tree :order => "name"
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
Example:
|
|
12
|
+
root
|
|
13
|
+
\_ child1
|
|
14
|
+
\_ subchild1
|
|
15
|
+
\_ subchild2
|
|
16
|
+
|
|
17
|
+
root = Category.create("name" => "root")
|
|
18
|
+
child1 = root.children.create("name" => "child1")
|
|
19
|
+
subchild1 = child1.children.create("name" => "subchild1")
|
|
20
|
+
|
|
21
|
+
root.parent # => nil
|
|
22
|
+
child1.parent # => root
|
|
23
|
+
root.children # => [child1]
|
|
24
|
+
root.children.first.children.first # => subchild1
|
|
25
|
+
|
|
26
|
+
Copyright (c) 2007 David Heinemeier Hansson, released under the MIT license
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
require 'rake'
|
|
2
|
+
require 'rake/testtask'
|
|
3
|
+
require 'rake/rdoctask'
|
|
4
|
+
|
|
5
|
+
desc 'Default: run unit tests.'
|
|
6
|
+
task :default => :test
|
|
7
|
+
|
|
8
|
+
desc 'Test acts_as_tree plugin.'
|
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
|
10
|
+
t.libs << 'lib'
|
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
|
12
|
+
t.verbose = true
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
desc 'Generate documentation for acts_as_tree plugin.'
|
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
|
18
|
+
rdoc.title = 'acts_as_tree'
|
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
|
20
|
+
rdoc.rdoc_files.include('README')
|
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
22
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ActiveRecord::Base.send :include, ActiveRecord::Acts::Tree
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
module ActiveRecord
|
|
2
|
+
module Acts
|
|
3
|
+
module Tree
|
|
4
|
+
def self.included(base)
|
|
5
|
+
base.extend(ClassMethods)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
# Specify this +acts_as+ extension if you want to model a tree structure by providing a parent association and a children
|
|
9
|
+
# association. This requires that you have a foreign key column, which by default is called +parent_id+.
|
|
10
|
+
#
|
|
11
|
+
# class Category < ActiveRecord::Base
|
|
12
|
+
# acts_as_tree :order => "name"
|
|
13
|
+
# end
|
|
14
|
+
#
|
|
15
|
+
# Example:
|
|
16
|
+
# root
|
|
17
|
+
# \_ child1
|
|
18
|
+
# \_ subchild1
|
|
19
|
+
# \_ subchild2
|
|
20
|
+
#
|
|
21
|
+
# root = Category.create("name" => "root")
|
|
22
|
+
# child1 = root.children.create("name" => "child1")
|
|
23
|
+
# subchild1 = child1.children.create("name" => "subchild1")
|
|
24
|
+
#
|
|
25
|
+
# root.parent # => nil
|
|
26
|
+
# child1.parent # => root
|
|
27
|
+
# root.children # => [child1]
|
|
28
|
+
# root.children.first.children.first # => subchild1
|
|
29
|
+
#
|
|
30
|
+
# In addition to the parent and children associations, the following instance methods are added to the class
|
|
31
|
+
# after calling <tt>acts_as_tree</tt>:
|
|
32
|
+
# * <tt>siblings</tt> - Returns all the children of the parent, excluding the current node (<tt>[subchild2]</tt> when called on <tt>subchild1</tt>)
|
|
33
|
+
# * <tt>self_and_siblings</tt> - Returns all the children of the parent, including the current node (<tt>[subchild1, subchild2]</tt> when called on <tt>subchild1</tt>)
|
|
34
|
+
# * <tt>ancestors</tt> - Returns all the ancestors of the current node (<tt>[child1, root]</tt> when called on <tt>subchild2</tt>)
|
|
35
|
+
# * <tt>root</tt> - Returns the root of the current node (<tt>root</tt> when called on <tt>subchild2</tt>)
|
|
36
|
+
module ClassMethods
|
|
37
|
+
# Configuration options are:
|
|
38
|
+
#
|
|
39
|
+
# * <tt>foreign_key</tt> - specifies the column name to use for tracking of the tree (default: +parent_id+)
|
|
40
|
+
# * <tt>order</tt> - makes it possible to sort the children according to this SQL snippet.
|
|
41
|
+
# * <tt>counter_cache</tt> - keeps a count in a +children_count+ column if set to +true+ (default: +false+).
|
|
42
|
+
def acts_as_tree(options = {})
|
|
43
|
+
configuration = { :foreign_key => "parent_id", :order => nil, :counter_cache => nil }
|
|
44
|
+
configuration.update(options) if options.is_a?(Hash)
|
|
45
|
+
|
|
46
|
+
belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key], :counter_cache => configuration[:counter_cache]
|
|
47
|
+
has_many :children, :class_name => name, :foreign_key => configuration[:foreign_key], :order => configuration[:order], :dependent => :destroy
|
|
48
|
+
|
|
49
|
+
class_eval <<-EOV
|
|
50
|
+
include ActiveRecord::Acts::Tree::InstanceMethods
|
|
51
|
+
|
|
52
|
+
def self.roots
|
|
53
|
+
find(:all, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}})
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def self.root
|
|
57
|
+
find(:first, :conditions => "#{configuration[:foreign_key]} IS NULL", :order => #{configuration[:order].nil? ? "nil" : %Q{"#{configuration[:order]}"}})
|
|
58
|
+
end
|
|
59
|
+
EOV
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
module InstanceMethods
|
|
64
|
+
# Returns list of ancestors, starting from parent until root.
|
|
65
|
+
#
|
|
66
|
+
# subchild1.ancestors # => [child1, root]
|
|
67
|
+
def ancestors
|
|
68
|
+
node, nodes = self, []
|
|
69
|
+
nodes << node = node.parent while node.parent
|
|
70
|
+
nodes
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Returns the root node of the tree.
|
|
74
|
+
def root
|
|
75
|
+
node = self
|
|
76
|
+
node = node.parent while node.parent
|
|
77
|
+
node
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Returns all siblings of the current node.
|
|
81
|
+
#
|
|
82
|
+
# subchild1.siblings # => [subchild2]
|
|
83
|
+
def siblings
|
|
84
|
+
self_and_siblings - [self]
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Returns all siblings and a reference to the current node.
|
|
88
|
+
#
|
|
89
|
+
# subchild1.self_and_siblings # => [subchild1, subchild2]
|
|
90
|
+
def self_and_siblings
|
|
91
|
+
parent ? parent.children : self.class.roots
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|