card 0.0.1 → 1.15.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (514) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +4 -0
  3. data/GPL +331 -0
  4. data/LICENSE +16 -0
  5. data/VERSION +1 -0
  6. data/card.gemspec +48 -19
  7. data/config/initializers/01_init_ruby_extensions.rb +7 -0
  8. data/config/initializers/inflections.rb +13 -0
  9. data/config/initializers/mime_types.rb +13 -0
  10. data/config/initializers/notification.rb +4 -0
  11. data/config/initializers/recaptcha.rb +6 -0
  12. data/config/initializers/uuid_state_file.rb +3 -0
  13. data/db/bootstrap/card_actions.yml +3872 -0
  14. data/db/bootstrap/card_acts.yml +7 -0
  15. data/db/bootstrap/card_changes.yml +11909 -0
  16. data/db/bootstrap/card_references.yml +3970 -0
  17. data/db/bootstrap/cards.yml +10805 -0
  18. data/db/migrate/20110511221913_require_earlier_migrations.rb +19 -0
  19. data/db/migrate/20120105203350_require_1_8_migrations.rb +15 -0
  20. data/db/migrate/20121111025347_require_1_10_migrations.rb +15 -0
  21. data/db/migrate/20121118114000_split_link_type.rb +16 -0
  22. data/db/migrate/20121118115000_update_link_type.rb +19 -0
  23. data/db/migrate/20130106052640_table_cleanup.rb +25 -0
  24. data/db/migrate/20130109015336_trunk_left.rb +14 -0
  25. data/db/migrate/20130411210957_update_codenames.rb +14 -0
  26. data/db/migrate/20140822073704_create_new_revision_tables.rb +43 -0
  27. data/db/migrate/20141001105348_move_revisions_to_actions.rb +62 -0
  28. data/db/migrate/20141121172918_rename_card_migration_table.rb +15 -0
  29. data/db/migrate/20141208132159_remove_present_from_reference_table.rb +9 -0
  30. data/db/migrate/20141216053032_better_index_names.rb +24 -0
  31. data/db/migrate_core_cards/20130411191151_renaming_for_menu.rb +49 -0
  32. data/db/migrate_core_cards/20130411211600_delete_old_related_tab_cards.rb +18 -0
  33. data/db/migrate_core_cards/20130419215612_import_help_text.rb +13 -0
  34. data/db/migrate_core_cards/20130823192433_add_style_cards.rb +87 -0
  35. data/db/migrate_core_cards/20130910183318_move_styles_to_content.rb +11 -0
  36. data/db/migrate_core_cards/20130920214038_jsonize_tinymce.rb +15 -0
  37. data/db/migrate_core_cards/20130920291703_update_stylesheets.rb +18 -0
  38. data/db/migrate_core_cards/20130927191728_account_events.rb +21 -0
  39. data/db/migrate_core_cards/20131016172445_common_css_patch.rb +12 -0
  40. data/db/migrate_core_cards/20140110193325_reset_account_request_type.rb +11 -0
  41. data/db/migrate_core_cards/20140307231621_user_data_to_cards.rb +73 -0
  42. data/db/migrate_core_cards/20140317035504_account_requests_to_signups.rb +51 -0
  43. data/db/migrate_core_cards/20140512155840_add_script_cards.rb +83 -0
  44. data/db/migrate_core_cards/20140629222005_add_email_cards.rb +134 -0
  45. data/db/migrate_core_cards/20140725180118_config_card_updates.rb +9 -0
  46. data/db/migrate_core_cards/20141111083921_delete_machine_output.rb +9 -0
  47. data/db/migrate_core_cards/20141115034214_config_descriptions_etc.rb +14 -0
  48. data/db/migrate_core_cards/20141119001955_make_symlinks_relative.rb +15 -0
  49. data/db/migrate_core_cards/20141120120605_fix_notification_html_message.rb +10 -0
  50. data/db/migrate_core_cards/20141204061304_watchers_to_following.rb +38 -0
  51. data/db/migrate_core_cards/20141208132416_partial_reference_type.rb +7 -0
  52. data/db/migrate_core_cards/20141208162106_add_ace_script.rb +12 -0
  53. data/db/migrate_core_cards/20141216155251_add_more_following_cards.rb +24 -0
  54. data/db/migrate_core_cards/20141230204340_uri_codename.rb +13 -0
  55. data/db/migrate_core_cards/20150202143810_import_bootstrap_layout.rb +17 -0
  56. data/db/migrate_core_cards/20150220134731_following_to_follow_rule.rb +18 -0
  57. data/db/migrate_core_cards/data/1.11_help_text.json +410 -0
  58. data/db/migrate_core_cards/data/1.12_stylesheets/classic_cards.scss +100 -0
  59. data/db/migrate_core_cards/data/1.12_stylesheets/common.scss +226 -0
  60. data/db/migrate_core_cards/data/1.12_stylesheets/right_sidebar.scss +27 -0
  61. data/db/migrate_core_cards/data/1.12_stylesheets/traditional.scss +152 -0
  62. data/db/migrate_core_cards/data/1.13_config_text.json +32 -0
  63. data/db/migrate_core_cards/data/1.14_config_descriptions_etc.json +77 -0
  64. data/db/migrate_core_cards/data/bootstrap_layout.json +81 -0
  65. data/db/migrate_core_cards/data/mailer/follower_notification_email.html +9 -0
  66. data/db/migrate_core_cards/data/mailer/follower_notification_email.txt +11 -0
  67. data/db/migrate_core_cards/data/mailer/mail_config.json +22 -0
  68. data/db/migrate_core_cards/data/mailer/password_reset_email.html +10 -0
  69. data/db/migrate_core_cards/data/mailer/password_reset_email.txt +11 -0
  70. data/db/migrate_core_cards/data/mailer/signup_alert_email.html +7 -0
  71. data/db/migrate_core_cards/data/mailer/signup_alert_email.txt +5 -0
  72. data/db/migrate_core_cards/data/mailer/verification_email.html +9 -0
  73. data/db/migrate_core_cards/data/mailer/verification_email.txt +8 -0
  74. data/db/schema.rb +128 -0
  75. data/db/seeds.rb +8 -0
  76. data/db/version.txt +1 -0
  77. data/db/version_core_cards.txt +1 -0
  78. data/lib/card.rb +46 -3
  79. data/lib/card/active_record_ext.rb +77 -0
  80. data/lib/card/auth.rb +181 -0
  81. data/lib/card/cache.rb +199 -0
  82. data/lib/card/chunk.rb +104 -0
  83. data/lib/card/codename.rb +72 -0
  84. data/lib/card/content.rb +211 -0
  85. data/lib/card/core_ext.rb +105 -0
  86. data/lib/card/core_migration.rb +6 -0
  87. data/lib/card/diff.rb +430 -0
  88. data/lib/card/env.rb +52 -0
  89. data/lib/card/exceptions.rb +44 -0
  90. data/lib/card/format.rb +563 -0
  91. data/lib/card/loader.rb +165 -0
  92. data/lib/card/log.rb +450 -0
  93. data/lib/card/mailer.rb +37 -0
  94. data/lib/card/migration.rb +145 -0
  95. data/lib/card/name.rb +45 -0
  96. data/lib/card/query.rb +81 -0
  97. data/lib/card/query/card_clause.rb +527 -0
  98. data/lib/card/query/clause.rb +21 -0
  99. data/lib/card/query/ref_clause.rb +47 -0
  100. data/lib/card/query/value_clause.rb +65 -0
  101. data/lib/card/reference.rb +63 -0
  102. data/lib/card/set.rb +369 -0
  103. data/lib/card/set_pattern.rb +168 -0
  104. data/lib/card/simplecov_helper.rb +61 -0
  105. data/lib/card/spec_helper.rb +68 -0
  106. data/lib/card/version.rb +9 -2
  107. data/lib/cardio.rb +145 -0
  108. data/lib/generators/card.rb +32 -0
  109. data/lib/generators/card/format/USAGE +9 -0
  110. data/lib/generators/card/format/format_generator.rb +27 -0
  111. data/lib/generators/card/format/templates/format_spec_template.erb +5 -0
  112. data/lib/generators/card/format/templates/format_template.erb +3 -0
  113. data/lib/generators/card/migration/USAGE +24 -0
  114. data/lib/generators/card/migration/migration_generator.rb +38 -0
  115. data/lib/generators/card/migration/templates/card_migration.erb +10 -0
  116. data/lib/generators/card/set/USAGE +15 -0
  117. data/lib/generators/card/set/set_generator.rb +29 -0
  118. data/lib/generators/card/set/templates/set_spec_template.erb +5 -0
  119. data/lib/generators/card/set/templates/set_template.erb +1 -0
  120. data/mod/01_core/chunk/include.rb +85 -0
  121. data/mod/01_core/chunk/link.rb +87 -0
  122. data/mod/01_core/chunk/literal.rb +24 -0
  123. data/mod/01_core/chunk/reference.rb +53 -0
  124. data/mod/01_core/chunk/uri.rb +135 -0
  125. data/mod/01_core/format/data_format.rb +2 -0
  126. data/mod/01_core/format/html_format.rb +129 -0
  127. data/mod/01_core/format/text_format.rb +10 -0
  128. data/mod/01_core/layout/blank.html +5 -0
  129. data/mod/01_core/layout/default.html +20 -0
  130. data/mod/01_core/layout/noside.html +16 -0
  131. data/mod/01_core/layout/pre.html +2 -0
  132. data/mod/01_core/layout/simple.html +5 -0
  133. data/mod/01_core/set/all/active_card.rb +35 -0
  134. data/mod/01_core/set/all/collection.rb +142 -0
  135. data/mod/01_core/set/all/content.rb +64 -0
  136. data/mod/01_core/set/all/erb.rb +11 -0
  137. data/mod/01_core/set/all/fetch.rb +228 -0
  138. data/mod/01_core/set/all/haml.rb +7 -0
  139. data/mod/01_core/set/all/initialize.rb +49 -0
  140. data/mod/01_core/set/all/name.rb +275 -0
  141. data/mod/01_core/set/all/pattern.rb +56 -0
  142. data/mod/01_core/set/all/permissions.rb +284 -0
  143. data/mod/01_core/set/all/phases.rb +185 -0
  144. data/mod/01_core/set/all/references.rb +118 -0
  145. data/mod/01_core/set/all/rules.rb +315 -0
  146. data/mod/01_core/set/all/states.rb +16 -0
  147. data/mod/01_core/set/all/templating.rb +120 -0
  148. data/mod/01_core/set/all/tracked_attributes.rb +114 -0
  149. data/mod/01_core/set/all/trash.rb +50 -0
  150. data/mod/01_core/set/all/type.rb +85 -0
  151. data/mod/01_core/set/all/utils.rb +85 -0
  152. data/mod/01_core/set_pattern/01_all.rb +11 -0
  153. data/mod/01_core/set_pattern/02_all_plus.rb +13 -0
  154. data/mod/01_core/set_pattern/03_type.rb +23 -0
  155. data/mod/01_core/set_pattern/04_star.rb +15 -0
  156. data/mod/01_core/set_pattern/05_rstar.rb +17 -0
  157. data/mod/01_core/set_pattern/06_right.rb +20 -0
  158. data/mod/01_core/set_pattern/07_type_plus_right.rb +23 -0
  159. data/mod/01_core/set_pattern/08_self.rb +19 -0
  160. data/mod/01_core/spec/chunk/literal_spec.rb +14 -0
  161. data/mod/01_core/spec/chunk/uri_spec.rb +292 -0
  162. data/mod/01_core/spec/format/data_format_spec.rb +5 -0
  163. data/mod/01_core/spec/format/html_format_spec.rb +140 -0
  164. data/mod/01_core/spec/format/text_format_spec.rb +5 -0
  165. data/mod/01_core/spec/set/all/active_card_spec.rb +5 -0
  166. data/mod/01_core/spec/set/all/attribute_tracking_spec.rb +21 -0
  167. data/mod/01_core/spec/set/all/collection_spec.rb +65 -0
  168. data/mod/01_core/spec/set/all/content_spec.rb +15 -0
  169. data/mod/01_core/spec/set/all/fetch_spec.rb +204 -0
  170. data/mod/01_core/spec/set/all/initialize_spec.rb +58 -0
  171. data/mod/01_core/spec/set/all/name_spec.rb +61 -0
  172. data/mod/01_core/spec/set/all/pattern_spec.rb +81 -0
  173. data/mod/01_core/spec/set/all/permissions_spec.rb +505 -0
  174. data/mod/01_core/spec/set/all/phases_spec.rb +6 -0
  175. data/mod/01_core/spec/set/all/references_spec.rb +8 -0
  176. data/mod/01_core/spec/set/all/rules2_spec.rb +250 -0
  177. data/mod/01_core/spec/set/all/rules_spec.rb +130 -0
  178. data/mod/01_core/spec/set/all/states_spec.rb +5 -0
  179. data/mod/01_core/spec/set/all/templating_spec.rb +111 -0
  180. data/mod/01_core/spec/set/all/tracked_attributes_spec.rb +328 -0
  181. data/mod/01_core/spec/set/all/trash_spec.rb +34 -0
  182. data/mod/01_core/spec/set/all/type_spec.rb +71 -0
  183. data/mod/01_core/spec/set/all/utils_spec.rb +5 -0
  184. data/mod/01_history/lib/card/act.rb +60 -0
  185. data/mod/01_history/lib/card/action.rb +176 -0
  186. data/mod/01_history/lib/card/change.rb +29 -0
  187. data/mod/01_history/set/all/actions.rb +47 -0
  188. data/mod/01_history/set/all/content_history.rb +131 -0
  189. data/mod/01_history/set/all/history.rb +296 -0
  190. data/mod/02_basic_types/format/css_format.rb +6 -0
  191. data/mod/02_basic_types/format/csv_format.rb +6 -0
  192. data/mod/02_basic_types/format/file_format.rb +3 -0
  193. data/mod/02_basic_types/format/js_format.rb +6 -0
  194. data/mod/02_basic_types/format/json_format.rb +6 -0
  195. data/mod/02_basic_types/format/rss_format.rb +11 -0
  196. data/mod/02_basic_types/format/xml_format.rb +6 -0
  197. data/mod/02_basic_types/set/all/all_css.rb +42 -0
  198. data/mod/02_basic_types/set/all/all_csv.rb +52 -0
  199. data/mod/02_basic_types/set/all/all_js.rb +7 -0
  200. data/mod/02_basic_types/set/all/base.rb +122 -0
  201. data/mod/02_basic_types/set/all/file.rb +13 -0
  202. data/mod/02_basic_types/set/all/json.rb +66 -0
  203. data/mod/02_basic_types/set/all/rss.rb +72 -0
  204. data/mod/02_basic_types/set/all/text.rb +8 -0
  205. data/mod/02_basic_types/set/type/plain_text.rb +10 -0
  206. data/mod/02_basic_types/set/type/pointer.rb +323 -0
  207. data/mod/02_basic_types/spec/set/all/all_css_spec.rb +10 -0
  208. data/mod/02_basic_types/spec/set/all/all_csv_spec.rb +9 -0
  209. data/mod/02_basic_types/spec/set/all/base_spec.rb +57 -0
  210. data/mod/02_basic_types/spec/set/all/file_spec.rb +5 -0
  211. data/mod/02_basic_types/spec/set/all/json_spec.rb +26 -0
  212. data/mod/02_basic_types/spec/set/all/rss_spec.rb +8 -0
  213. data/mod/02_basic_types/spec/set/all/text_spec.rb +5 -0
  214. data/mod/02_basic_types/spec/set/type/plain_text_spec.rb +11 -0
  215. data/mod/02_basic_types/spec/set/type/pointer_spec.rb +103 -0
  216. data/mod/03_machines/lib/card/machine.rb +185 -0
  217. data/mod/03_machines/lib/card/machine_input.rb +40 -0
  218. data/mod/03_machines/lib/javascript/ace.js +18204 -0
  219. data/mod/03_machines/lib/javascript/html5shiv-printshiv.js +1 -0
  220. data/mod/03_machines/lib/javascript/jquery-ui.js +14913 -0
  221. data/mod/03_machines/lib/javascript/jquery.autosize.js +274 -0
  222. data/mod/03_machines/lib/javascript/jquery.fileupload.js +1114 -0
  223. data/mod/03_machines/lib/javascript/jquery.iframe-transport.js +185 -0
  224. data/mod/03_machines/lib/javascript/jquery.js +4 -0
  225. data/mod/03_machines/lib/javascript/jquery.ui.autocomplete.html.js +41 -0
  226. data/mod/03_machines/lib/javascript/jquery_ujs.js +469 -0
  227. data/mod/03_machines/lib/javascript/jquerymobile.js +15454 -0
  228. data/mod/03_machines/lib/javascript/theme-textmate.js +130 -0
  229. data/mod/03_machines/lib/javascript/tinymce.js +13 -0
  230. data/mod/03_machines/lib/javascript/wagn.js.coffee +336 -0
  231. data/mod/03_machines/lib/javascript/wagn_menu.js +72 -0
  232. data/mod/03_machines/lib/javascript/wagn_mod.js.coffee +446 -0
  233. data/mod/03_machines/lib/stylesheets/functional.scss +184 -0
  234. data/mod/03_machines/lib/stylesheets/jquery-ui-smoothness.css +1178 -0
  235. data/mod/03_machines/lib/stylesheets/standard.scss +760 -0
  236. data/mod/03_machines/set/right/machine_output.rb +20 -0
  237. data/mod/03_machines/set/self/script_ace.rb +8 -0
  238. data/mod/03_machines/set/self/script_card_menu.rb +8 -0
  239. data/mod/03_machines/set/self/script_html5shiv_printshiv.rb +7 -0
  240. data/mod/03_machines/set/self/script_jquery.rb +8 -0
  241. data/mod/03_machines/set/self/script_jquery_helper.rb +14 -0
  242. data/mod/03_machines/set/self/script_slot.rb +9 -0
  243. data/mod/03_machines/set/self/script_tinymce.rb +8 -0
  244. data/mod/03_machines/set/self/style_functional.rb +8 -0
  245. data/mod/03_machines/set/self/style_jquery_ui_smoothness.rb +8 -0
  246. data/mod/03_machines/set/self/style_standard.rb +8 -0
  247. data/mod/03_machines/set/type/coffee_script.rb +61 -0
  248. data/mod/03_machines/set/type/css.rb +45 -0
  249. data/mod/03_machines/set/type/java_script.rb +38 -0
  250. data/mod/03_machines/set/type/scss.rb +23 -0
  251. data/mod/03_machines/set/type/skin.rb +12 -0
  252. data/mod/03_machines/spec/lib/shared_machine_examples.rb +175 -0
  253. data/mod/03_machines/spec/lib/shared_machine_input_examples.rb +65 -0
  254. data/mod/03_machines/spec/set/right/machine_output_spec.rb +5 -0
  255. data/mod/03_machines/spec/set/self/style_functional_spec.rb +5 -0
  256. data/mod/03_machines/spec/set/self/style_jquery_ui_smoothness_spec.rb +5 -0
  257. data/mod/03_machines/spec/set/self/style_standard_spec.rb +5 -0
  258. data/mod/03_machines/spec/set/type/coffeescript_spec.rb +29 -0
  259. data/mod/03_machines/spec/set/type/css_spec.rb +34 -0
  260. data/mod/03_machines/spec/set/type/javascript_spec.rb +28 -0
  261. data/mod/03_machines/spec/set/type/scss_spec.rb +56 -0
  262. data/mod/03_machines/spec/set/type/skin_spec.rb +70 -0
  263. data/mod/04_settings/lib/card/setting.rb +65 -0
  264. data/mod/04_settings/set/right/add_help.rb +11 -0
  265. data/mod/04_settings/set/right/comment.rb +91 -0
  266. data/mod/04_settings/set/right/create.rb +3 -0
  267. data/mod/04_settings/set/right/default.rb +3 -0
  268. data/mod/04_settings/set/right/delete.rb +4 -0
  269. data/mod/04_settings/set/right/help.rb +3 -0
  270. data/mod/04_settings/set/right/read.rb +2 -0
  271. data/mod/04_settings/set/right/script.rb +10 -0
  272. data/mod/04_settings/set/right/structure.rb +4 -0
  273. data/mod/04_settings/set/right/style.rb +10 -0
  274. data/mod/04_settings/set/right/update.rb +2 -0
  275. data/mod/04_settings/set/self/accountable.rb +3 -0
  276. data/mod/04_settings/set/self/add_help.rb +2 -0
  277. data/mod/04_settings/set/self/autoname.rb +2 -0
  278. data/mod/04_settings/set/self/captcha.rb +2 -0
  279. data/mod/04_settings/set/self/comment.rb +2 -0
  280. data/mod/04_settings/set/self/create.rb +2 -0
  281. data/mod/04_settings/set/self/default.rb +2 -0
  282. data/mod/04_settings/set/self/delete.rb +2 -0
  283. data/mod/04_settings/set/self/help.rb +2 -0
  284. data/mod/04_settings/set/self/input.rb +2 -0
  285. data/mod/04_settings/set/self/layout.rb +2 -0
  286. data/mod/04_settings/set/self/on_create.rb +2 -0
  287. data/mod/04_settings/set/self/on_delete.rb +2 -0
  288. data/mod/04_settings/set/self/on_update.rb +2 -0
  289. data/mod/04_settings/set/self/options.rb +2 -0
  290. data/mod/04_settings/set/self/options_label.rb +2 -0
  291. data/mod/04_settings/set/self/read.rb +2 -0
  292. data/mod/04_settings/set/self/script.rb +2 -0
  293. data/mod/04_settings/set/self/structure.rb +2 -0
  294. data/mod/04_settings/set/self/style.rb +2 -0
  295. data/mod/04_settings/set/self/table_of_contents.rb +2 -0
  296. data/mod/04_settings/set/self/thanks.rb +2 -0
  297. data/mod/04_settings/set/self/update.rb +2 -0
  298. data/mod/04_settings/set/type/setting.rb +82 -0
  299. data/mod/04_settings/spec/set/right/add_help_spec.rb +5 -0
  300. data/mod/04_settings/spec/set/right/comment_spec.rb +41 -0
  301. data/mod/04_settings/spec/set/right/create_spec.rb +10 -0
  302. data/mod/04_settings/spec/set/right/default_spec.rb +5 -0
  303. data/mod/04_settings/spec/set/right/delete_spec.rb +5 -0
  304. data/mod/04_settings/spec/set/right/help_spec.rb +5 -0
  305. data/mod/04_settings/spec/set/right/read_spec.rb +5 -0
  306. data/mod/04_settings/spec/set/right/script_spec.rb +24 -0
  307. data/mod/04_settings/spec/set/right/structure_spec.rb +17 -0
  308. data/mod/04_settings/spec/set/right/style_spec.rb +29 -0
  309. data/mod/04_settings/spec/set/right/update_spec.rb +5 -0
  310. data/mod/04_settings/spec/set/type/setting_spec.rb +10 -0
  311. data/mod/05_email/format/email_html_format.rb +9 -0
  312. data/mod/05_email/format/email_text_format.rb +11 -0
  313. data/mod/05_email/lib/card/follow_option.rb +90 -0
  314. data/mod/05_email/set/all/email_html.rb +5 -0
  315. data/mod/05_email/set/all/email_text.rb +5 -0
  316. data/mod/05_email/set/all/follow.rb +256 -0
  317. data/mod/05_email/set/all/notify.rb +223 -0
  318. data/mod/05_email/set/all/observer.rb +27 -0
  319. data/mod/05_email/set/right/bcc.rb +45 -0
  320. data/mod/05_email/set/right/cc.rb +3 -0
  321. data/mod/05_email/set/right/follow.rb +9 -0
  322. data/mod/05_email/set/right/follow_fields.rb +3 -0
  323. data/mod/05_email/set/right/followers.rb +30 -0
  324. data/mod/05_email/set/right/following.rb +52 -0
  325. data/mod/05_email/set/right/from.rb +3 -0
  326. data/mod/05_email/set/right/html_message.rb +3 -0
  327. data/mod/05_email/set/right/to.rb +3 -0
  328. data/mod/05_email/set/self/always.rb +14 -0
  329. data/mod/05_email/set/self/created.rb +21 -0
  330. data/mod/05_email/set/self/edited.rb +24 -0
  331. data/mod/05_email/set/self/follow.rb +2 -0
  332. data/mod/05_email/set/self/follow_defaults.rb +66 -0
  333. data/mod/05_email/set/self/never.rb +15 -0
  334. data/mod/05_email/set/type/email_template.rb +113 -0
  335. data/mod/05_email/set/type_plus_right/user/follow.rb +176 -0
  336. data/mod/05_email/spec/set/all/follow_spec.rb +133 -0
  337. data/mod/05_email/spec/set/all/notify_spec.rb +364 -0
  338. data/mod/05_email/spec/set/all/observer_spec.rb +76 -0
  339. data/mod/05_email/spec/set/right/followers_spec.rb +126 -0
  340. data/mod/05_email/spec/set/right/following_spec.rb +4 -0
  341. data/mod/05_email/spec/set/self/follow_defaults_spec.rb +18 -0
  342. data/mod/05_standard/file/103/icon-6566.ico +0 -0
  343. data/mod/05_standard/file/103/large-6566.ico +0 -0
  344. data/mod/05_standard/file/103/medium-6566.ico +0 -0
  345. data/mod/05_standard/file/103/original-6566.ico +0 -0
  346. data/mod/05_standard/file/103/small-6566.ico +0 -0
  347. data/mod/05_standard/file/79/icon-6556.png +0 -0
  348. data/mod/05_standard/file/79/large-6556.png +0 -0
  349. data/mod/05_standard/file/79/medium-6556.png +0 -0
  350. data/mod/05_standard/file/79/original-6556.png +0 -0
  351. data/mod/05_standard/file/79/small-6556.png +0 -0
  352. data/mod/05_standard/file/790/icon-6419.png +0 -0
  353. data/mod/05_standard/file/790/large-6419.png +0 -0
  354. data/mod/05_standard/file/790/medium-6419.png +0 -0
  355. data/mod/05_standard/file/790/original-6419.png +0 -0
  356. data/mod/05_standard/file/790/small-6419.png +0 -0
  357. data/mod/05_standard/set/all/account.rb +67 -0
  358. data/mod/05_standard/set/all/attach.rb +152 -0
  359. data/mod/05_standard/set/all/comment.rb +39 -0
  360. data/mod/05_standard/set/all/error.rb +214 -0
  361. data/mod/05_standard/set/all/event_viz.rb +62 -0
  362. data/mod/05_standard/set/all/links.rb +110 -0
  363. data/mod/05_standard/set/all/rich_html/content.rb +173 -0
  364. data/mod/05_standard/set/all/rich_html/editing.rb +145 -0
  365. data/mod/05_standard/set/all/rich_html/form.rb +234 -0
  366. data/mod/05_standard/set/all/rich_html/header.rb +64 -0
  367. data/mod/05_standard/set/all/rich_html/wrapper.rb +105 -0
  368. data/mod/05_standard/set/right/account.rb +180 -0
  369. data/mod/05_standard/set/right/email.rb +52 -0
  370. data/mod/05_standard/set/right/password.rb +39 -0
  371. data/mod/05_standard/set/right/salt.rb +5 -0
  372. data/mod/05_standard/set/right/stats.rb +35 -0
  373. data/mod/05_standard/set/right/status.rb +9 -0
  374. data/mod/05_standard/set/right/token.rb +5 -0
  375. data/mod/05_standard/set/right/when_created.rb +5 -0
  376. data/mod/05_standard/set/right/when_last_edited.rb +5 -0
  377. data/mod/05_standard/set/rstar/rules.rb +407 -0
  378. data/mod/05_standard/set/self/account_links.rb +61 -0
  379. data/mod/05_standard/set/self/alerts.rb +5 -0
  380. data/mod/05_standard/set/self/all.rb +21 -0
  381. data/mod/05_standard/set/self/foot.rb +8 -0
  382. data/mod/05_standard/set/self/head.rb +121 -0
  383. data/mod/05_standard/set/self/navbox.rb +20 -0
  384. data/mod/05_standard/set/self/now.rb +6 -0
  385. data/mod/05_standard/set/self/recent.rb +48 -0
  386. data/mod/05_standard/set/self/search.rb +43 -0
  387. data/mod/05_standard/set/self/signin.rb +121 -0
  388. data/mod/05_standard/set/self/stats.rb +91 -0
  389. data/mod/05_standard/set/self/version.rb +9 -0
  390. data/mod/05_standard/set/type/basic.rb +46 -0
  391. data/mod/05_standard/set/type/cardtype.rb +43 -0
  392. data/mod/05_standard/set/type/date.rb +8 -0
  393. data/mod/05_standard/set/type/file.rb +85 -0
  394. data/mod/05_standard/set/type/html.rb +22 -0
  395. data/mod/05_standard/set/type/image.rb +67 -0
  396. data/mod/05_standard/set/type/layout_type.rb +14 -0
  397. data/mod/05_standard/set/type/number.rb +19 -0
  398. data/mod/05_standard/set/type/phrase.rb +3 -0
  399. data/mod/05_standard/set/type/search_type.rb +243 -0
  400. data/mod/05_standard/set/type/set.rb +198 -0
  401. data/mod/05_standard/set/type/signup.rb +144 -0
  402. data/mod/05_standard/set/type/toggle.rb +12 -0
  403. data/mod/05_standard/set/type/uri.rb +11 -0
  404. data/mod/05_standard/set/type/user.rb +72 -0
  405. data/mod/05_standard/spec/chunk/include_spec.rb +186 -0
  406. data/mod/05_standard/spec/chunk/link_spec.rb +61 -0
  407. data/mod/05_standard/spec/format/css_format_spec.rb +5 -0
  408. data/mod/05_standard/spec/format/csv_format_spec.rb +5 -0
  409. data/mod/05_standard/spec/format/email_html_format_spec.rb +5 -0
  410. data/mod/05_standard/spec/format/file_format_spec.rb +5 -0
  411. data/mod/05_standard/spec/format/js_format_spec.rb +5 -0
  412. data/mod/05_standard/spec/format/json_format_spec.rb +5 -0
  413. data/mod/05_standard/spec/format/rss_format_spec.rb +5 -0
  414. data/mod/05_standard/spec/format/xml_format_spec.rb +5 -0
  415. data/mod/05_standard/spec/set/all/account_spec.rb +137 -0
  416. data/mod/05_standard/spec/set/all/attach_spec.rb +9 -0
  417. data/mod/05_standard/spec/set/all/comment_spec.rb +5 -0
  418. data/mod/05_standard/spec/set/all/email_html_spec.rb +15 -0
  419. data/mod/05_standard/spec/set/all/error_spec.rb +9 -0
  420. data/mod/05_standard/spec/set/all/event_viz_spec.rb +9 -0
  421. data/mod/05_standard/spec/set/all/history_spec.rb +173 -0
  422. data/mod/05_standard/spec/set/all/rich_html/form_spec.rb +43 -0
  423. data/mod/05_standard/spec/set/all/rich_html/wrapper_spec.rb +28 -0
  424. data/mod/05_standard/spec/set/right/account_spec.rb +162 -0
  425. data/mod/05_standard/spec/set/right/email_spec.rb +55 -0
  426. data/mod/05_standard/spec/set/right/password_spec.rb +45 -0
  427. data/mod/05_standard/spec/set/right/salt_spec.rb +5 -0
  428. data/mod/05_standard/spec/set/right/stats_spec.rb +13 -0
  429. data/mod/05_standard/spec/set/right/status_spec.rb +5 -0
  430. data/mod/05_standard/spec/set/right/token_spec.rb +5 -0
  431. data/mod/05_standard/spec/set/right/when_created_spec.rb +7 -0
  432. data/mod/05_standard/spec/set/right/when_last_edited_spec.rb +7 -0
  433. data/mod/05_standard/spec/set/rstar/rules_spec.rb +25 -0
  434. data/mod/05_standard/spec/set/self/account_links_spec.rb +9 -0
  435. data/mod/05_standard/spec/set/self/alerts_spec.rb +5 -0
  436. data/mod/05_standard/spec/set/self/all_spec.rb +48 -0
  437. data/mod/05_standard/spec/set/self/foot_spec.rb +5 -0
  438. data/mod/05_standard/spec/set/self/head_spec.rb +17 -0
  439. data/mod/05_standard/spec/set/self/navbox_spec.rb +7 -0
  440. data/mod/05_standard/spec/set/self/now_spec.rb +7 -0
  441. data/mod/05_standard/spec/set/self/recent_spec.rb +5 -0
  442. data/mod/05_standard/spec/set/self/search_spec.rb +5 -0
  443. data/mod/05_standard/spec/set/self/signin_spec.rb +73 -0
  444. data/mod/05_standard/spec/set/self/stats_spec.rb +12 -0
  445. data/mod/05_standard/spec/set/self/version_spec.rb +7 -0
  446. data/mod/05_standard/spec/set/type/basic_spec.rb +5 -0
  447. data/mod/05_standard/spec/set/type/cardtype_spec.rb +5 -0
  448. data/mod/05_standard/spec/set/type/date_spec.rb +7 -0
  449. data/mod/05_standard/spec/set/type/email_template_spec.rb +208 -0
  450. data/mod/05_standard/spec/set/type/file_spec.rb +5 -0
  451. data/mod/05_standard/spec/set/type/html_spec.rb +23 -0
  452. data/mod/05_standard/spec/set/type/image_spec.rb +16 -0
  453. data/mod/05_standard/spec/set/type/layout_type_spec.rb +7 -0
  454. data/mod/05_standard/spec/set/type/number_spec.rb +7 -0
  455. data/mod/05_standard/spec/set/type/phrase_spec.rb +7 -0
  456. data/mod/05_standard/spec/set/type/search_type_spec.rb +27 -0
  457. data/mod/05_standard/spec/set/type/set_spec.rb +26 -0
  458. data/mod/05_standard/spec/set/type/signup_spec.rb +228 -0
  459. data/mod/05_standard/spec/set/type/toggle_spec.rb +12 -0
  460. data/mod/05_standard/spec/set/type/uri_spec.rb +41 -0
  461. data/mod/05_standard/spec/set/type/user_spec.rb +5 -0
  462. data/mod/06_bootstrap/lib/javascript/bootstrap.js +2306 -0
  463. data/mod/06_bootstrap/lib/javascript/bootstrap.min.js +7 -0
  464. data/mod/06_bootstrap/lib/stylesheets/bootstrap-theme.css +476 -0
  465. data/mod/06_bootstrap/lib/stylesheets/bootstrap.css +6565 -0
  466. data/mod/06_bootstrap/lib/stylesheets/darkly.css +6583 -0
  467. data/mod/06_bootstrap/set/all/bootstrap/form.rb +37 -0
  468. data/mod/06_bootstrap/set/all/bootstrap/header.rb +29 -0
  469. data/mod/06_bootstrap/set/all/bootstrap/wrapper.rb +12 -0
  470. data/mod/06_bootstrap/set/all/rich_bootstrap.rb +28 -0
  471. data/mod/06_bootstrap/set/self/bootstrap_css.rb +7 -0
  472. data/mod/06_bootstrap/set/self/bootstrap_js.rb +7 -0
  473. data/mod/06_bootstrap/set/self/bootstrap_theme_css.rb +7 -0
  474. data/mod/06_bootstrap/set/self/navbox.rb +32 -0
  475. data/mod/06_bootstrap/set/type/search_type.rb +79 -0
  476. data/mod/06_bootstrap/spec/set/all/bootstrap/form_spec.rb +13 -0
  477. data/spec/lib/card/action_spec.rb +14 -0
  478. data/spec/lib/card/auth_spec.rb +17 -0
  479. data/spec/lib/card/cache_spec.rb +122 -0
  480. data/spec/lib/card/chunk_spec.rb +17 -0
  481. data/spec/lib/card/codename_spec.rb +25 -0
  482. data/spec/lib/card/content_spec.rb +314 -0
  483. data/spec/lib/card/diff_spec.rb +210 -0
  484. data/spec/lib/card/format_spec.rb +82 -0
  485. data/spec/lib/card/loader_spec.rb +39 -0
  486. data/spec/lib/card/log_spec.rb +270 -0
  487. data/spec/lib/card/name_spec.rb +279 -0
  488. data/spec/lib/card/query_spec.rb +456 -0
  489. data/spec/lib/card/reference_spec.rb +213 -0
  490. data/spec/lib/card/set_pattern_spec.rb +56 -0
  491. data/spec/lib/card/set_spec.rb +88 -0
  492. data/spec/mailers/mailer_spec.rb +64 -0
  493. data/spec/models/card/cardtype_spec.rb +216 -0
  494. data/spec/models/card/create_spec.rb +82 -0
  495. data/spec/models/card/trash_spec.rb +260 -0
  496. data/spec/models/card/type_transition_spec.rb +161 -0
  497. data/spec/models/card/validation_spec.rb +36 -0
  498. data/spec/models/card_spec.rb +177 -0
  499. data/spec/spec_helper.rb +130 -0
  500. data/test/fixtures/.gitkeep +0 -0
  501. data/test/fixtures/card_actions.yml +5706 -0
  502. data/test/fixtures/card_acts.yml +835 -0
  503. data/test/fixtures/card_changes.yml +17182 -0
  504. data/test/fixtures/card_references.yml +5405 -0
  505. data/test/fixtures/cards.yml +15533 -0
  506. data/test/fixtures/mao2.jpg +0 -0
  507. data/test/fixtures/rails.gif +0 -0
  508. data/test/seed.rb +201 -0
  509. metadata +762 -38
  510. data/.gitignore +0 -17
  511. data/Gemfile +0 -4
  512. data/LICENSE.txt +0 -22
  513. data/README.md +0 -29
  514. data/Rakefile +0 -1
@@ -0,0 +1,37 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'open-uri'
3
+
4
+
5
+ class Card
6
+ class Mailer < ActionMailer::Base
7
+
8
+ @@defaults = Card.config.email_defaults || {}
9
+ @@defaults.symbolize_keys!
10
+ @@defaults[:return_path] ||= @@defaults[:from] if @@defaults[:from]
11
+ @@defaults[:charset] ||= 'utf-8'
12
+ default @@defaults
13
+
14
+ class << self
15
+ def new_mail(*args, &block)
16
+ mail = Mail.new(args, &block)
17
+ method = Card::Mailer.delivery_method
18
+ mail.delivery_method(method, Card::Mailer.send(:"#{method}_settings"))
19
+ mail.perform_deliveries = Card::Mailer.perform_deliveries
20
+ mail.raise_delivery_errors = Card::Mailer.raise_delivery_errors
21
+ mail
22
+ end
23
+
24
+ def layout message
25
+ %{
26
+ <!DOCTYPE html>
27
+ <html>
28
+ <body>
29
+ #{message}
30
+ </body>
31
+ </html>
32
+ }
33
+ end
34
+ end
35
+ end
36
+ end
37
+
@@ -0,0 +1,145 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ class Card::Migration < ActiveRecord::Migration
4
+ @type = :deck_cards
5
+
6
+ class << self
7
+
8
+ # Rake tasks use class methods, migrations use instance methods.
9
+ # To avoid repetition a lot of instance methods here just call class methods.
10
+ # The subclass Card::CoreMigration needs a different @type so we can't use a
11
+ # class variable @@type. It has to be a class instance variable.
12
+ # Migrations are subclasses of Card::Migration or Card::CoreMigration but they
13
+ # don't inherit the @type. The method below solves this problem.
14
+ def type
15
+ @type || (ancestors[1] && ancestors[1].type)
16
+ end
17
+
18
+ def find_unused_name base_name
19
+ test_name = base_name
20
+ add = 1
21
+ while Card.exists?(test_name) do
22
+ test_name = "#{base_name}#{add}"
23
+ add +=1
24
+ end
25
+ test_name
26
+ end
27
+
28
+ def migration_paths mig_type=type
29
+ Cardio.migration_paths mig_type
30
+ end
31
+
32
+ def schema mig_type=type
33
+ Cardio.schema mig_type
34
+ end
35
+
36
+ def schema_suffix mig_type=type
37
+ Cardio.schema_suffix mig_type
38
+ end
39
+
40
+ def schema_mode mig_type=type
41
+ new_suffix = schema_suffix mig_type
42
+ original_suffix = ActiveRecord::Base.table_name_suffix
43
+
44
+ ActiveRecord::Base.table_name_suffix = new_suffix
45
+ yield
46
+ ActiveRecord::Base.table_name_suffix = original_suffix
47
+ end
48
+
49
+ def assume_migrated_upto_version
50
+ schema_mode do
51
+ ActiveRecord::Schema.assume_migrated_upto_version schema, migration_paths
52
+ end
53
+ end
54
+
55
+ def data_path filename=nil
56
+ path = migration_paths.first
57
+ File.join( [ migration_paths.first, 'data', filename ].compact )
58
+ end
59
+
60
+ end
61
+
62
+ def contentedly &block
63
+ Card::Cache.reset_global
64
+ Cardio.schema_mode '' do
65
+ Card::Auth.as_bot do
66
+ ActiveRecord::Base.transaction do
67
+ begin
68
+ yield
69
+ ensure
70
+ Card::Cache.reset_global
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ def import_json filename
78
+ Card.config.action_mailer.perform_deliveries = false
79
+ raw_json = File.read( data_path filename )
80
+ json = JSON.parse raw_json
81
+ Card.merge_list json["card"]["value"], :output_file=>File.join(data_path,"unmerged_#{ filename }")
82
+ end
83
+
84
+ def data_path filename=nil
85
+ self.class.data_path filename
86
+ end
87
+
88
+ def schema_mode
89
+ Cardio.schema_mode self.class.type
90
+ end
91
+
92
+ def migration_paths
93
+ Cardio.paths self.class.type
94
+ end
95
+
96
+
97
+ # Execute this migration in the named direction
98
+ # copied from ActiveRecord to wrap "up" in "contentendly"
99
+ def migrate(direction)
100
+ return unless respond_to?(direction)
101
+
102
+ case direction
103
+ when :up then announce "migrating"
104
+ when :down then announce "reverting"
105
+ end
106
+
107
+ time = nil
108
+ ActiveRecord::Base.connection_pool.with_connection do |conn|
109
+ @connection = conn
110
+ if respond_to?(:change)
111
+ if direction == :down
112
+ recorder = CommandRecorder.new(@connection)
113
+ suppress_messages do
114
+ @connection = recorder
115
+ change
116
+ end
117
+ @connection = conn
118
+ time = Benchmark.measure {
119
+ self.revert {
120
+ recorder.inverse.each do |cmd, args|
121
+ send(cmd, *args)
122
+ end
123
+ }
124
+ }
125
+ else
126
+ time = Benchmark.measure { change }
127
+ end
128
+ else
129
+ time = Benchmark.measure { contentedly { send(direction) } }
130
+ end
131
+ @connection = nil
132
+ end
133
+
134
+ case direction
135
+ when :up then announce "migrated (%.4fs)" % time.real; write
136
+ when :down then announce "reverted (%.4fs)" % time.real; write
137
+ end
138
+ end
139
+
140
+ def down
141
+ raise ActiveRecord::IrreversibleMigration
142
+ end
143
+ end
144
+
145
+ require 'card/core_migration'
data/lib/card/name.rb ADDED
@@ -0,0 +1,45 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require_dependency 'card/env'
3
+
4
+ require 'smart_name'
5
+
6
+ class Card
7
+ class Name < SmartName
8
+
9
+ self.params = Card::Env # yuck!
10
+ self.session = proc { Card::Auth.current.name }
11
+ self.banned_array = ['/']
12
+
13
+ def star?
14
+ simple? and '*' == s[0,1]
15
+ end
16
+
17
+ def rstar?
18
+ right and '*' == right[0,1]
19
+ end
20
+
21
+ def trait_name? *traitlist
22
+ junction? && begin
23
+ right_key = right_name.key
24
+ !!traitlist.find do |codename|
25
+ card_id = Card::Codename[ codename ] and card = Card.fetch( card_id, :skip_modules=>true, :skip_virtual=>true ) and
26
+ card.key == right_key
27
+ end
28
+ end
29
+ end
30
+
31
+ def trait_name tag_code
32
+ card_id = Card::Codename[ tag_code ] and card = Card.fetch( card_id, :skip_modules=>true, :skip_virtual=>true ) and
33
+ [ self, card.cardname ].to_name
34
+ end
35
+
36
+ def trait tag_code
37
+ name = trait_name( tag_code )
38
+ name ? name.s : ( raise Card::NotFound, "unknown codename: #{tag_code}" )
39
+ end
40
+
41
+ def code
42
+ Card::Codename[ Card.fetch_id self ]
43
+ end
44
+ end
45
+ end
data/lib/card/query.rb ADDED
@@ -0,0 +1,81 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ class Card::Query
4
+ require_dependency 'card/query/clause'
5
+ require_dependency 'card/query/card_clause'
6
+ require_dependency 'card/query/value_clause'
7
+ require_dependency 'card/query/ref_clause'
8
+
9
+ MODIFIERS = {}; %w{ conj return sort sort_as group dir limit offset }.each{|key| MODIFIERS[key.to_sym] = nil }
10
+
11
+ OPERATORS = %w{ != = =~ < > in ~ }.inject({}) {|h,v| h[v]=nil; h }.merge({
12
+ :eq => '=', :gt => '>', :lt => '<',
13
+ :match => '~', :ne => '!=', 'not in' => nil
14
+ }.stringify_keys)
15
+
16
+ def initialize query
17
+ @card_clause = CardClause.build query
18
+ end
19
+
20
+ def query
21
+ @card_clause.query
22
+ end
23
+
24
+ def sql
25
+ @sql ||= @card_clause.to_sql
26
+ end
27
+
28
+ def run
29
+ # puts "~~~~~~~~~~~~~~\nCARD SPEC =\n#{@card_clause.rawclause}\n\n-----\n\nSQL=\n#{sql}"
30
+ rows = ActiveRecord::Base.connection.select_all( sql )
31
+ retrn = query[:return].present? ? query[:return].to_s : 'card'
32
+ case retrn
33
+ when 'card'
34
+ rows.map do |row|
35
+ card=
36
+ if query[:prepend] || query[:append]
37
+ cardname = [query[:prepend], row['name'], query[:append]].compact.join('+')
38
+ Card.fetch cardname, :new=>{}
39
+ else
40
+ Card[ row['name'] ]
41
+ end
42
+ card.nil? ? Card.find_by_name_and_trash(row['name'],false).repair_key : card
43
+ end
44
+ when 'count'
45
+ rows.first['count'].to_i
46
+ when 'raw'
47
+ rows
48
+ else
49
+ integer = ( retrn =~ /id$/ )
50
+ rows.map do |row|
51
+ integer ? row[retrn].to_i : row[retrn]
52
+ end
53
+ end
54
+ end
55
+
56
+
57
+
58
+ class SqlCond < String
59
+ def to_sql(*args) self end
60
+ end
61
+
62
+
63
+ class SqlStatement
64
+ attr_accessor :fields, :tables, :joins, :conditions, :group, :order, :limit, :offset, :distinct
65
+
66
+ def initialize
67
+ @fields, @joins, @conditions = [],[],[],[]
68
+ @tables = @group = @order = @limit = @offset = @distinct = nil
69
+ end
70
+
71
+ def to_s
72
+ select = fields.reject(&:blank?) * ', '
73
+ where = conditions.reject(&:blank?) * ' and '
74
+
75
+ ['(SELECT', distinct, select, 'FROM', tables, joins, 'WHERE', where, group, order, limit, offset, ')'].compact * ' '
76
+ end
77
+ end
78
+
79
+
80
+ end
81
+
@@ -0,0 +1,527 @@
1
+
2
+ class Card
3
+ class Query
4
+ class CardClause < Clause
5
+
6
+ ATTRIBUTES = {
7
+ :basic => %w{ name type_id content id key updater_id left_id right_id creator_id updater_id codename },
8
+ :relational => %w{ type part left right editor_of edited_by last_editor_of last_edited_by creator_of created_by member_of member },
9
+ :plus_relational => %w{ plus left_plus right_plus },
10
+ :ref_relational => %w{ refer_to referred_to_by link_to linked_to_by include included_by },
11
+ :conjunction => %w{ and or all any },
12
+ :special => %w{ found_by not sort match complete extension_type },
13
+ :ignore => %w{ prepend append view params vars size }
14
+ }.inject({}) {|h,pair| pair[1].each {|v| h[v.to_sym]=pair[0] }; h }
15
+
16
+ DEFAULT_ORDER_DIRS = { :update => "desc", :relevance => "desc" }
17
+ CONJUNCTIONS = { :any=>:or, :in=>:or, :or=>:or, :all=>:and, :and=>:and }
18
+
19
+ attr_reader :sql, :query, :rawclause, :selfname
20
+ attr_accessor :joins, :join_count
21
+
22
+ class << self
23
+ def build query
24
+ cardclause = self.new query
25
+ cardclause.merge cardclause.rawclause
26
+ end
27
+ end
28
+
29
+ def initialize query
30
+ @mods = MODIFIERS.clone
31
+ @clause, @joins = {}, {}
32
+ @selfname, @parent = '', nil
33
+ @sql = SqlStatement.new
34
+
35
+ @query = query.clone
36
+ @query.merge! @query.delete(:params) if @query[:params]
37
+ @vars = @query.delete(:vars) || {}
38
+ @vars.symbolize_keys!
39
+ @query = clean(@query)
40
+ @rawclause = @query.deep_clone
41
+
42
+ @sql.distinct = 'DISTINCT' if @parent
43
+
44
+ self
45
+ end
46
+
47
+
48
+ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49
+ # QUERY CLEANING - strip strings, absolutize names, interpret contextual parameters
50
+ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
51
+
52
+
53
+ def clean query
54
+ query = query.symbolize_keys
55
+ if s = query.delete(:context) then @selfname = s end
56
+ if p = query.delete(:_parent) then @parent = p end
57
+ query.each do |key,val|
58
+ query[key] = clean_val val
59
+ end
60
+ query
61
+ end
62
+
63
+ def clean_val val
64
+ case val
65
+ when String
66
+ if val =~ /^\$(\w+)$/
67
+ val = @vars[$1.to_sym].to_s.strip
68
+ end
69
+ absolute_name val
70
+ when Card::Name ; clean_val val.s
71
+ when Hash ; clean val
72
+ when Array ; val.map { |v| clean_val v }
73
+ when Integer, Float, Symbol ; val
74
+ else ; raise BadQuery, "unknown WQL value type: #{val.class}"
75
+ end
76
+ end
77
+
78
+ def root
79
+ @parent ? @parent.root : self
80
+ end
81
+
82
+ def absolute_name name
83
+ name =~ /\b_/ ? name.to_name.to_absolute(root.selfname) : name
84
+ end
85
+
86
+
87
+ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
88
+ # MERGE - reduce query to basic attributes and SQL subconditions
89
+ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
90
+
91
+
92
+ def merge s
93
+ s = hashify s
94
+ translate_to_attributes s
95
+ ready_to_sqlize s
96
+ @clause.merge! s
97
+ self
98
+ end
99
+
100
+ def hashify s
101
+ case s
102
+ when String; { :key => s.to_name.key }
103
+ when Integer; { :id => s }
104
+ when Hash; s
105
+ else; raise BadQuery, "Invalid cardclause args #{s.inspect}"
106
+ end
107
+ end
108
+
109
+ def translate_to_attributes clause
110
+ content = nil
111
+ clause.each do |key,val|
112
+ if key == :_parent
113
+ @parent = clause.delete(key)
114
+ elsif OPERATORS.has_key?(key.to_s) && !ATTRIBUTES[key]
115
+ clause.delete(key)
116
+ content = [key,val]
117
+ elsif MODIFIERS.has_key?(key)
118
+ next if clause[key].is_a? Hash
119
+ val = clause.delete key
120
+ @mods[key] = Array === val ? val : val.to_s
121
+ end
122
+ end
123
+ clause[:content] = content if content
124
+ end
125
+
126
+
127
+ def ready_to_sqlize clause
128
+ clause.each do |key,val|
129
+ keyroot = field_root(key).to_sym
130
+ if keyroot==:cond # internal SQL cond (already ready)
131
+ elsif ATTRIBUTES[keyroot] == :basic # sqlize knows how to handle these keys; just process value
132
+ clause[key] = ValueClause.new(val, self)
133
+ else # keys need additional processing
134
+ val = clause.delete key
135
+ is_array = Array===val
136
+ case ATTRIBUTES[keyroot]
137
+ when :ignore #noop
138
+ when :relational, :special, :conjunction ; relate is_array, keyroot, val, :send
139
+ when :ref_relational ; relate is_array, keyroot, val, :refclause
140
+ when :plus_relational
141
+ # Arrays can have multiple interpretations for these, so we have to look closer...
142
+ subcond = is_array && ( Array===val.first || conjunction(val.first) )
143
+
144
+ relate subcond, keyroot, val, :send
145
+ else ; raise BadQuery, "Invalid attribute #{key}"
146
+ end
147
+ end
148
+ end
149
+
150
+ end
151
+
152
+ def relate subcond, key, val, method
153
+ if subcond
154
+ conj = conjunction( val.first ) ? conjunction( val.shift ) : :and
155
+ if conj == current_conjunction # same conjunction as container, no need for subcondition
156
+ val.each { |v| send method, key, v }
157
+ else
158
+ send conj, val.inject({}) { |h,v| h[field key] = v; h } # subcondition
159
+ end
160
+ else
161
+ send method, key, val
162
+ end
163
+ end
164
+
165
+ def refclause key, val
166
+ add_join :ref, RefClause.new( key, val, self ).to_sql, :id, :ref_id
167
+ end
168
+
169
+
170
+ def conjunction val
171
+ if [String, Symbol].member? val.class
172
+ CONJUNCTIONS[val.to_sym]
173
+ end
174
+ end
175
+
176
+
177
+ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
178
+ # ATTRIBUTE METHODS - called during merge
179
+ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
180
+
181
+
182
+ #~~~~~~ RELATIONAL
183
+
184
+ def type val
185
+ restrict :type_id, val
186
+ end
187
+
188
+ def part val
189
+ right = Integer===val ? val : val.clone
190
+ subcondition :left=>val, :right=>right, :conj=>:or
191
+ end
192
+
193
+
194
+ def left val
195
+ restrict :left_id, val
196
+ end
197
+
198
+ def right val
199
+ restrict :right_id, val
200
+ end
201
+
202
+ def editor_of val
203
+ action_clause :actor_id, "card_actions.card_id", val
204
+ end
205
+
206
+ def edited_by val
207
+ action_clause "card_actions.card_id", :actor_id, val
208
+ end
209
+
210
+ def last_editor_of val
211
+ restrict_by_join :id, val, :return=>'updater_id'
212
+ end
213
+
214
+ def last_edited_by val
215
+ restrict :updater_id, val
216
+ end
217
+
218
+ def creator_of val
219
+ restrict_by_join :id, val, :return=>'creator_id'
220
+ end
221
+
222
+ def created_by val
223
+ restrict :creator_id, val
224
+ end
225
+
226
+ def member_of val
227
+ merge field(:right_plus) => [RolesID, {:refer_to=>val}]
228
+ end
229
+
230
+ def member val
231
+ merge field(:referred_to_by) => {:left=>val, :right=>RolesID }
232
+ end
233
+
234
+
235
+ #~~~~~~ PLUS RELATIONAL
236
+
237
+ def left_plus val
238
+ junction :left, val
239
+ end
240
+
241
+ def right_plus val
242
+ junction :right, val
243
+ end
244
+
245
+ def plus val
246
+ any( { :left_plus=>val, :right_plus=>val.deep_clone } )
247
+ end
248
+
249
+ def junction side, val
250
+ part_clause, junction_clause = val.is_a?(Array) ? val : [ val, {} ]
251
+ restrict_by_join :id, junction_clause, side=>part_clause, :return=>"#{ side==:left ? :right : :left}_id"
252
+ end
253
+
254
+
255
+ #~~~~~~~ CONJUNCTION
256
+
257
+ def and val
258
+ subcondition val
259
+ end
260
+ alias :all :and
261
+
262
+ def or val
263
+ subcondition val, :conj=>:or
264
+ end
265
+ alias :any :or
266
+
267
+ #~~~~~~ SPECIAL
268
+
269
+
270
+ def found_by val
271
+
272
+ cards = if Hash===val
273
+ Query.new(val).run
274
+ else
275
+ Array.wrap(val).map do |v|
276
+ Card.fetch absolute_name(val), :new=>{}
277
+ end
278
+ end
279
+
280
+ cards.each do |c|
281
+ unless c && [SearchTypeID,SetID].include?(c.type_id)
282
+ raise BadQuery, %{"found_by" value needs to be valid Search, but #{c.name} is a #{c.type_name}}
283
+ end
284
+ restrict_by_join :id, CardClause.new(c.get_query).rawclause
285
+ end
286
+ end
287
+
288
+ def not val
289
+ subselect = CardClause.build(:return=>:id, :_parent=>self).merge(val).to_sql
290
+ join_alias = add_join :not, subselect, :id, :id, :side=>'LEFT'
291
+ merge field(:cond) => SqlCond.new("#{join_alias}.id is null")
292
+ end
293
+
294
+ def sort val
295
+ return nil if @parent
296
+ val[:return] = val[:return] ? safe_sql(val[:return]) : 'db_content'
297
+ item = val.delete(:item) || 'left'
298
+
299
+ if val[:return] == 'count'
300
+ cs_args = { :return=>'count', :group=>'sort_join_field', :_parent=>self }
301
+ @mods[:sort] = "coalesce(count,0)" # needed for postgres
302
+ case item
303
+ when 'referred_to'
304
+ join_field = 'id'
305
+ cs = CardClause.build cs_args.merge( field(:cond)=>SqlCond.new("referer_id in #{CardClause.build( val.merge(:return=>'id')).to_sql}") )
306
+ cs.add_join :wr, :card_references, :id, :referee_id
307
+ else
308
+ raise BadQuery, "count with item: #{item} not yet implemented"
309
+ end
310
+ else
311
+ join_field = case item
312
+ when 'left' ; 'left_id'
313
+ when 'right' ; 'right_id'
314
+ else ; raise BadQuery, "sort item: #{item} not yet implemented"
315
+ end
316
+ cs = CardClause.build(val)
317
+ end
318
+
319
+ cs.sql.fields << "#{cs.table_alias}.#{join_field} as sort_join_field"
320
+ join_table = add_join :sort, cs.to_sql, :id, :sort_join_field, :side=>'LEFT'
321
+ @mods[:sort] ||= "#{join_table}.#{val[:return]}"
322
+
323
+ end
324
+
325
+ def match(val)
326
+ cxn, val = match_prep val
327
+ val.gsub! /[^#{Card::Name::OK4KEY_RE}]+/, ' '
328
+ return nil if val.strip.empty?
329
+
330
+
331
+ cond = begin
332
+ val_list = val.split(/\s+/).map do |v|
333
+ name_or_content = ["replace(#{self.table_alias}.name,'+',' ')","#{self.table_alias}.db_content"].map do |field|
334
+ %{#{field} #{ cxn.match quote("[[:<:]]#{v}[[:>:]]") }}
335
+ end
336
+ "(#{name_or_content.join ' OR '})"
337
+ end
338
+ "(#{val_list.join ' AND '})"
339
+ end
340
+
341
+ merge field(:cond)=>SqlCond.new(cond)
342
+ end
343
+
344
+
345
+ def complete(val)
346
+ no_plus_card = (val=~/\+/ ? '' : "and right_id is null") #FIXME -- this should really be more nuanced -- it breaks down after one plus
347
+ merge field(:cond) => SqlCond.new(" lower(name) LIKE lower(#{quote(val.to_s+'%')}) #{no_plus_card}")
348
+ end
349
+
350
+ def extension_type val
351
+ # DEPRECATED LONG AGO!!!
352
+ Rails.logger.info "using DEPRECATED extension_type in WQL"
353
+ merge field(:right_plus) => AccountID
354
+ end
355
+
356
+
357
+ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
358
+ # ATTRIBUTE METHOD HELPERS - called by attribute methods above
359
+ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
360
+
361
+
362
+ def table_alias
363
+ case
364
+ when @mods[:return]=='condition'
365
+ @parent ? @parent.table_alias : "t"
366
+ when @parent
367
+ @parent.table_alias + "x"
368
+ else
369
+ "t"
370
+ end
371
+ end
372
+
373
+ def add_join(name, table, cardfield, otherfield, opts={})
374
+ root.join_count = root.join_count.to_i + 1
375
+ join_alias = "#{name}_#{root.join_count}"
376
+ on = "#{table_alias}.#{cardfield} = #{join_alias}.#{otherfield}"
377
+ #is_subselect = !table.is_a?( Symbol )
378
+
379
+ if @mods[:conj] == 'or' #and is_subselect
380
+ opts[:side] ||= 'LEFT'
381
+ merge field(:cond) => SqlCond.new(on)
382
+ end
383
+ @joins[join_alias] = ["\n ", opts[:side], 'JOIN', table, 'AS', join_alias, 'ON', on, "\n"].compact.join ' '
384
+ join_alias
385
+ end
386
+
387
+ def field name
388
+ @fields ||= {}
389
+ @fields[name] ||= 0
390
+ @fields[name] += 1
391
+ "#{ name }_#{ @fields[name] }"
392
+ end
393
+
394
+ def field_root key
395
+ key.to_s.gsub /\_\d+/, ''
396
+ end
397
+
398
+ def subcondition(val, args={})
399
+ args = { :return=>:condition, :_parent=>self }.merge(args)
400
+ cardclause = CardClause.build( args )
401
+ merge field(:cond) => cardclause.merge(val)
402
+ self.joins.merge! cardclause.joins
403
+ end
404
+
405
+ def action_clause(field, linkfield, val)
406
+ card_select = CardClause.build(:_parent=>self, :return=>'id').merge(val).to_sql
407
+ sql = "(SELECT DISTINCT #{field} AS join_card_id FROM card_acts INNER JOIN card_actions ON card_acts.id = card_act_id "
408
+ sql += " JOIN (#{card_select}) AS ss ON #{linkfield}=ss.id AND (draft is not true))"
409
+ add_join :ac, sql, :id, :join_card_id
410
+ end
411
+
412
+ def id_from_clause clause
413
+ case clause
414
+ when Integer ; clause
415
+ when String ; Card.fetch_id(clause)
416
+ end
417
+ end
418
+
419
+ def restrict id_field, val, opts={}
420
+ if id = id_from_clause(val)
421
+ merge field(id_field) => id
422
+ else
423
+ restrict_by_join id_field, val, opts
424
+ end
425
+ end
426
+
427
+ def restrict_by_join id_field, val, opts={}
428
+ opts.reverse_merge!(:return=>:id, :_parent=>self)
429
+ subselect = CardClause.build(opts).merge(val).to_sql
430
+ add_join "card_#{id_field}", subselect, id_field, opts[:return]
431
+ end
432
+
433
+ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
434
+ # SQL GENERATION - translate merged hash into complete SQL statement.
435
+ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
436
+
437
+
438
+ def to_sql *args
439
+ sql.conditions << basic_conditions
440
+
441
+ if @mods[:return]=='condition'
442
+ conds = sql.conditions.last
443
+ return conds.blank? ? nil : "(#{conds})"
444
+ end
445
+
446
+ if pconds = permission_conditions
447
+ sql.conditions << pconds
448
+ end
449
+
450
+ sql.fields.unshift fields_to_sql
451
+ sql.order = sort_to_sql # has side effects!
452
+ sql.tables = "cards #{table_alias}"
453
+ sql.joins += @joins.values
454
+
455
+ sql.conditions << "#{table_alias}.trash is false"
456
+
457
+ sql.group = "GROUP BY #{safe_sql(@mods[:group])}" if !@mods[:group].blank?
458
+ unless @parent or @mods[:return]=='count'
459
+ if @mods[:limit].to_i > 0
460
+ sql.limit = "LIMIT #{ @mods[:limit ].to_i }"
461
+ sql.offset = "OFFSET #{ @mods[:offset].to_i }" if !@mods[:offset].blank?
462
+ end
463
+ end
464
+
465
+ sql.to_s
466
+ end
467
+
468
+ def basic_conditions
469
+ @clause.map { |key, val| val.to_sql field_root(key) }.compact.join " #{ current_conjunction } "
470
+ end
471
+
472
+ def current_conjunction
473
+ @mods[:conj].blank? ? :and : @mods[:conj]
474
+ end
475
+
476
+ def permission_conditions
477
+ unless Auth.always_ok? #or ( Card::Query.root_perms_only && !root? )
478
+ read_rules = Auth.as_card.read_rules
479
+ read_rule_list = read_rules.nil? ? 1 : read_rules.join(',')
480
+ "(#{table_alias}.read_rule_id IN (#{ read_rule_list }))"
481
+ end
482
+ end
483
+
484
+ def fields_to_sql
485
+ field = @mods[:return]
486
+ case (field.blank? ? :card : field.to_sym)
487
+ when :raw; "#{table_alias}.*"
488
+ when :card; "#{table_alias}.name"
489
+ when :count; "coalesce(count(*),0) as count"
490
+ when :content; "#{table_alias}.db_content"
491
+ else
492
+ ATTRIBUTES[field.to_sym]==:basic ? "#{table_alias}.#{field}" : safe_sql(field)
493
+ end
494
+ end
495
+
496
+ def sort_to_sql
497
+ #fail "order_key = #{@mods[:sort]}, class = #{order_key.class}"
498
+
499
+ return nil if @parent or @mods[:return]=='count' #FIXME - extend to all root-only clauses
500
+ order_key ||= @mods[:sort].blank? ? "update" : @mods[:sort]
501
+
502
+ order_directives = [order_key].flatten.map do |key|
503
+ dir = @mods[:dir].blank? ? (DEFAULT_ORDER_DIRS[key.to_sym]||'asc') : safe_sql(@mods[:dir]) #wonky
504
+ sort_field key, @mods[:sort_as], dir
505
+ end.join ', '
506
+ "ORDER BY #{order_directives}"
507
+
508
+ end
509
+
510
+ def sort_field key, as, dir
511
+ order_field = case key
512
+ when "id"; "#{table_alias}.id"
513
+ when "update"; "#{table_alias}.updated_at"
514
+ when "create"; "#{table_alias}.created_at"
515
+ when /^(name|alpha)$/; "LOWER( #{table_alias}.key )"
516
+ when 'content'; "#{table_alias}.db_content"
517
+ when "relevance"; "#{table_alias}.updated_at" #deprecated
518
+ else
519
+ safe_sql(key)
520
+ end
521
+ order_field = "CAST(#{order_field} AS #{cast_type(as)})" if as
522
+ "#{order_field} #{dir}"
523
+
524
+ end
525
+ end
526
+ end
527
+ end