acts_as_mt_object 0.0.1

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.
Files changed (47) hide show
  1. data/.gitignore +8 -0
  2. data/Gemfile +19 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +56 -0
  5. data/Rakefile +49 -0
  6. data/acts_as_mt_object.gemspec +25 -0
  7. data/db/mysql.dump +2238 -0
  8. data/lib/acts_as_mt_object.rb +145 -0
  9. data/lib/acts_as_mt_object/version.rb +3 -0
  10. data/lib/tasks/acts_as_mt_object_tasks.rake +4 -0
  11. data/test/acts_as_mt_object_test.rb +38 -0
  12. data/test/dummy/Rakefile +7 -0
  13. data/test/dummy/app/assets/javascripts/application.js +9 -0
  14. data/test/dummy/app/assets/stylesheets/application.css +7 -0
  15. data/test/dummy/app/controllers/application_controller.rb +3 -0
  16. data/test/dummy/app/helpers/application_helper.rb +2 -0
  17. data/test/dummy/app/mailers/.gitkeep +0 -0
  18. data/test/dummy/app/models/.gitkeep +0 -0
  19. data/test/dummy/app/models/mt/entry.rb +6 -0
  20. data/test/dummy/app/models/plan.rb +4 -0
  21. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  22. data/test/dummy/config.ru +4 -0
  23. data/test/dummy/config/application.rb +45 -0
  24. data/test/dummy/config/boot.rb +10 -0
  25. data/test/dummy/config/database.yml +52 -0
  26. data/test/dummy/config/environment.rb +5 -0
  27. data/test/dummy/config/environments/development.rb +30 -0
  28. data/test/dummy/config/environments/production.rb +60 -0
  29. data/test/dummy/config/environments/test.rb +39 -0
  30. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  31. data/test/dummy/config/initializers/inflections.rb +10 -0
  32. data/test/dummy/config/initializers/mime_types.rb +5 -0
  33. data/test/dummy/config/initializers/secret_token.rb +7 -0
  34. data/test/dummy/config/initializers/session_store.rb +8 -0
  35. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  36. data/test/dummy/config/locales/en.yml +5 -0
  37. data/test/dummy/config/routes.rb +58 -0
  38. data/test/dummy/lib/assets/.gitkeep +0 -0
  39. data/test/dummy/lib/tasks/mt.rake +49 -0
  40. data/test/dummy/log/.gitkeep +0 -0
  41. data/test/dummy/public/404.html +26 -0
  42. data/test/dummy/public/422.html +26 -0
  43. data/test/dummy/public/500.html +26 -0
  44. data/test/dummy/public/favicon.ico +0 -0
  45. data/test/dummy/script/rails +6 -0
  46. data/test/test_helper.rb +10 -0
  47. metadata +161 -0
@@ -0,0 +1,145 @@
1
+ # ActsAsMtObject
2
+
3
+ module ActiveRecord
4
+ module Acts
5
+ module ActsAsMtObject
6
+
7
+ def self.included(base)
8
+ base.extend ClassMethods
9
+ end
10
+
11
+ module ClassMethods
12
+
13
+ def acts_as_mt_object(options = {}, &extension)
14
+ options = {
15
+ :mt_class => self.to_s
16
+ }.merge(options)
17
+
18
+ class_variable_set(
19
+ '@@mt_class', options[:mt_class].to_s.sub(/.*::/, '').downcase
20
+ )
21
+
22
+ send :include, ActiveRecord::Acts::ActsAsMtObject::ActMethods
23
+ end
24
+
25
+ end
26
+
27
+ module ActMethods
28
+
29
+ def self.included(base)
30
+ basename = base.class_variable_get('@@mt_class')
31
+ table_name = 'mt_' + basename
32
+ primary_key = basename + '_id'
33
+
34
+ custom_fields = []
35
+ meta_fields = []
36
+
37
+ base.class_eval do
38
+ set_table_name table_name
39
+ set_primary_key primary_key
40
+
41
+ imethods = instance_methods
42
+
43
+ column_names.each do |column|
44
+ prefix = basename + '_'
45
+ short_name = column.sub(prefix, '')
46
+ if ! imethods.include?(short_name.to_sym) && column != short_name
47
+ define_method short_name do
48
+ send column
49
+ end
50
+ end
51
+ end
52
+
53
+ results = nil
54
+ begin
55
+ results = connection.execute(<<-__SQL__
56
+ SELECT field_basename, field_type, field_default FROM mt_field
57
+ WHERE field_obj_type = '#{basename}'
58
+ __SQL__
59
+ )
60
+ rescue => e
61
+ end
62
+ if results
63
+ results.each do |field|
64
+ custom_fields << field
65
+ if ! imethods.include?(field[0].to_sym)
66
+ define_method field[0] do
67
+ row = meta_values[('field.' + field[0]).to_sym]
68
+ if row
69
+ case field[1]
70
+ when 'text','select', 'radio', 'checkbox'
71
+ row[1]
72
+ when 'textarea'
73
+ row[9]
74
+ else
75
+ row.find{|v| v}
76
+ end
77
+ end || field[2]
78
+ end
79
+ end
80
+ end
81
+ end
82
+ class_variable_set('@@custom_fields', custom_fields)
83
+
84
+ results = nil
85
+ begin
86
+ results = connection.execute(<<-__SQL__
87
+ SELECT #{basename}_meta_type FROM #{table_name}_meta
88
+ WHERE NOT #{basename}_meta_type LIKE 'field.%'
89
+ GROUP BY #{basename}_meta_type
90
+ __SQL__
91
+ )
92
+ rescue => e
93
+ end
94
+ if results
95
+ results.each do |field|
96
+ meta_fields << field
97
+ define_method field[0] do
98
+ row = meta_values[field[0].to_sym]
99
+ return row ? row.find{|v| v} : nil
100
+ end
101
+ end
102
+ end
103
+ class_variable_set('@@meta_fields', meta_fields)
104
+
105
+ def meta_values
106
+ if ! @meta_values
107
+ @meta_values = {}
108
+ if ! self.class.class_variable_get('@@custom_fields').blank? ||
109
+ ! self.class.class_variable_get('@@meta_fields').blank?
110
+
111
+ basename = self.class.class_variable_get('@@mt_class')
112
+ connection.execute(
113
+ "SELECT #{basename}_meta_type, " +
114
+ %W(
115
+ meta_vchar meta_vchar_idx meta_vdatetime
116
+ meta_vdatetime_idx meta_vinteger meta_vinteger_idx
117
+ meta_vfloat meta_vfloat_idx meta_vblob meta_vclob
118
+ ).map{|c| basename + '_' + c}.join(',') +
119
+ " FROM #{self.class.table_name}_meta " +
120
+ " WHERE #{basename}_meta_#{basename}_id = #{send self.class.primary_key}"
121
+ ).each do |row|
122
+ @meta_values[row.shift.to_sym] = row
123
+ end
124
+
125
+ end
126
+ end
127
+
128
+ return @meta_values
129
+ end
130
+
131
+ def self.custom_field_names
132
+ class_variable_get('@@custom_fields').map(&:first)
133
+ end
134
+
135
+ def self.meta_field_names
136
+ class_variable_get('@@meta_fields').map(&:first)
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
144
+
145
+ ActiveRecord::Base.send :include, ActiveRecord::Acts::ActsAsMtObject
@@ -0,0 +1,3 @@
1
+ module ActsAsMtObject
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :acts_as_mt_object do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ class ActsAsMtObjectTest < ActiveSupport::TestCase
4
+ test "module loaded" do
5
+ assert_kind_of Module, ActsAsMtObject
6
+ end
7
+
8
+ @@entry_data = {
9
+ 4 => {
10
+ :id => 4,
11
+ :title => '1st entry title',
12
+ :text => '1st entry body',
13
+ :current_revision => 1,
14
+ :price => 'unknown', # default
15
+ :deadline_date => '', # default
16
+ },
17
+ 5 => {
18
+ :id => 5,
19
+ :title => '2nd entry title',
20
+ :text => '2nd entry body',
21
+ :current_revision => 1,
22
+ :price => '30000',
23
+ :deadline_date => DateTime.parse('2012-02-01 10:10:00 UTC'),
24
+ }
25
+ }
26
+
27
+ [Plan, MT::Entry].each do |klass|
28
+ test klass.to_s do
29
+ for id, data in @@entry_data
30
+ entry = klass.find(id)
31
+ assert_not_nil(entry)
32
+ for key, value in data
33
+ assert_equal entry.send(key), value
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
3
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4
+
5
+ require File.expand_path('../config/application', __FILE__)
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,9 @@
1
+ // This is a manifest file that'll be compiled into including all the files listed below.
2
+ // Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
3
+ // be included in the compiled file accessible from http://example.com/assets/application.js
4
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
5
+ // the compiled file.
6
+ //
7
+ //= require jquery
8
+ //= require jquery_ujs
9
+ //= require_tree .
@@ -0,0 +1,7 @@
1
+ /*
2
+ * This is a manifest file that'll automatically include all the stylesheets available in this directory
3
+ * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
4
+ * the top of the compiled file, but it's generally better to create a new file per style scope.
5
+ *= require_self
6
+ *= require_tree .
7
+ */
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
File without changes
File without changes
@@ -0,0 +1,6 @@
1
+ module MT
2
+ class Entry < ActiveRecord::Base
3
+ establish_connection (::Rails.env + '_mt').to_sym
4
+ acts_as_mt_object
5
+ end
6
+ end
@@ -0,0 +1,4 @@
1
+ class Plan < ActiveRecord::Base
2
+ establish_connection (::Rails.env + '_mt').to_sym
3
+ acts_as_mt_object :mt_class => :Entry
4
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag "application" %>
6
+ <%= javascript_include_tag "application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Dummy::Application
@@ -0,0 +1,45 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ Bundler.require
6
+ require "acts_as_mt_object"
7
+
8
+ module Dummy
9
+ class Application < Rails::Application
10
+ # Settings in config/environments/* take precedence over those specified here.
11
+ # Application configuration should go into files in config/initializers
12
+ # -- all .rb files in that directory are automatically loaded.
13
+
14
+ # Custom directories with classes and modules you want to be autoloadable.
15
+ # config.autoload_paths += %W(#{config.root}/extras)
16
+
17
+ # Only load the plugins named here, in the order given (default is alphabetical).
18
+ # :all can be used as a placeholder for all plugins not explicitly named.
19
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
20
+
21
+ # Activate observers that should always be running.
22
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
23
+
24
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
25
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
26
+ # config.time_zone = 'Central Time (US & Canada)'
27
+
28
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
29
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
30
+ # config.i18n.default_locale = :de
31
+
32
+ # Configure the default encoding used in templates for Ruby 1.9.
33
+ config.encoding = "utf-8"
34
+
35
+ # Configure sensitive parameters which will be filtered from the log file.
36
+ config.filter_parameters += [:password]
37
+
38
+ # Enable the asset pipeline
39
+ config.assets.enabled = true
40
+
41
+ # Version of your assets, change this if you want to expire all your assets
42
+ config.assets.version = '1.0'
43
+ end
44
+ end
45
+
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ gemfile = File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ if File.exist?(gemfile)
5
+ ENV['BUNDLE_GEMFILE'] = gemfile
6
+ require 'bundler'
7
+ Bundler.setup
8
+ end
9
+
10
+ $:.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,52 @@
1
+ # MySQL. Versions 4.1 and 5.0 are recommended.
2
+ #
3
+ # Install the MYSQL driver
4
+ # gem install mysql2
5
+ #
6
+ # Ensure the MySQL gem is defined in your Gemfile
7
+ # gem 'mysql2'
8
+ #
9
+ # And be sure to use new-style password hashing:
10
+ # http://dev.mysql.com/doc/refman/5.0/en/old-client.html
11
+ development:
12
+ adapter: mysql2
13
+ encoding: utf8
14
+ reconnect: false
15
+ database: dummy_development
16
+ username: root
17
+ password:
18
+ charset: utf8
19
+ collation: utf8_unicode_ci
20
+
21
+ # Warning: The database defined as "test" will be erased and
22
+ # re-generated from your development database when you run "rake".
23
+ # Do not set this db to the same as development or production.
24
+ test:
25
+ adapter: mysql2
26
+ encoding: utf8
27
+ reconnect: false
28
+ database: dummy_test
29
+ username: root
30
+ password:
31
+ charset: utf8
32
+ collation: utf8_unicode_ci
33
+
34
+ test_mt:
35
+ adapter: mysql2
36
+ encoding: utf8
37
+ reconnect: false
38
+ database: dummy_test_mt
39
+ username: root
40
+ password:
41
+ charset: utf8
42
+ collation: utf8_unicode_ci
43
+
44
+ production:
45
+ adapter: mysql2
46
+ encoding: utf8
47
+ reconnect: false
48
+ database: dummy_production
49
+ username: root
50
+ password:
51
+ charset: utf8
52
+ collation: utf8_unicode_ci
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ Dummy::Application.initialize!
@@ -0,0 +1,30 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the web server when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Log error messages when you accidentally call methods on nil.
10
+ config.whiny_nils = true
11
+
12
+ # Show full error reports and disable caching
13
+ config.consider_all_requests_local = true
14
+ config.action_controller.perform_caching = false
15
+
16
+ # Don't care if the mailer can't send
17
+ config.action_mailer.raise_delivery_errors = false
18
+
19
+ # Print deprecation notices to the Rails logger
20
+ config.active_support.deprecation = :log
21
+
22
+ # Only use best-standards-support built into browsers
23
+ config.action_dispatch.best_standards_support = :builtin
24
+
25
+ # Do not compress assets
26
+ config.assets.compress = false
27
+
28
+ # Expands the lines which load the assets
29
+ config.assets.debug = true
30
+ end
@@ -0,0 +1,60 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # Code is not reloaded between requests
5
+ config.cache_classes = true
6
+
7
+ # Full error reports are disabled and caching is turned on
8
+ config.consider_all_requests_local = false
9
+ config.action_controller.perform_caching = true
10
+
11
+ # Disable Rails's static asset server (Apache or nginx will already do this)
12
+ config.serve_static_assets = false
13
+
14
+ # Compress JavaScripts and CSS
15
+ config.assets.compress = true
16
+
17
+ # Don't fallback to assets pipeline if a precompiled asset is missed
18
+ config.assets.compile = false
19
+
20
+ # Generate digests for assets URLs
21
+ config.assets.digest = true
22
+
23
+ # Defaults to Rails.root.join("public/assets")
24
+ # config.assets.manifest = YOUR_PATH
25
+
26
+ # Specifies the header that your server uses for sending files
27
+ # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
28
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
29
+
30
+ # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
31
+ # config.force_ssl = true
32
+
33
+ # See everything in the log (default is :info)
34
+ # config.log_level = :debug
35
+
36
+ # Use a different logger for distributed setups
37
+ # config.logger = SyslogLogger.new
38
+
39
+ # Use a different cache store in production
40
+ # config.cache_store = :mem_cache_store
41
+
42
+ # Enable serving of images, stylesheets, and JavaScripts from an asset server
43
+ # config.action_controller.asset_host = "http://assets.example.com"
44
+
45
+ # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
46
+ # config.assets.precompile += %w( search.js )
47
+
48
+ # Disable delivery errors, bad email addresses will be ignored
49
+ # config.action_mailer.raise_delivery_errors = false
50
+
51
+ # Enable threaded mode
52
+ # config.threadsafe!
53
+
54
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
55
+ # the I18n.default_locale when a translation can not be found)
56
+ config.i18n.fallbacks = true
57
+
58
+ # Send deprecation notices to registered listeners
59
+ config.active_support.deprecation = :notify
60
+ end