infopark_fiona_connector 6.8.0.beta.200.891.647580e

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,5 @@
1
+ = Infopark Fiona Connector
2
+
3
+ Infopark Fiona Connector integrates the Infopark Rails Connector with the Infopark CMS Fiona.
4
+
5
+
data/lib/comment.rb ADDED
@@ -0,0 +1,4 @@
1
+ # The Comment model inherits all of its behavior from
2
+ # RailsConnector::DefaultComment.
3
+ class Comment < RailsConnector::DefaultComment
4
+ end
@@ -0,0 +1,23 @@
1
+ require 'rails_connector/rack_middlewares'
2
+ require 'rails_connector/core_extensions'
3
+ require "rails_connector/errors"
4
+
5
+ module RailsConnector
6
+ def self.autoload_all_sources
7
+ source_files = Dir.glob(File.expand_path("../rails_connector/*.rb", __FILE__)).map do |file|
8
+ File.basename(file)
9
+ end
10
+
11
+ source_files.each do |file|
12
+ name = file.gsub(".rb", "")
13
+ autoload name.camelcase, "rails_connector/#{name}"
14
+ end
15
+ end
16
+
17
+ autoload_all_sources
18
+
19
+ autoload :AttrValueProvider, "rails_connector/attr_value_provider_loader"
20
+ end
21
+
22
+ autoload :Comment, "comment"
23
+ autoload :Rating, "rating"
@@ -0,0 +1,146 @@
1
+ require 'mime/types'
2
+ require 'ostruct'
3
+
4
+ module RailsConnector
5
+
6
+ # The page related and custom data belonging to a CMS object
7
+ #
8
+ # [editor] The login of the current editor of the edited content, if set
9
+ #
10
+ # Any custom Attribute can be accessed directly or via [].
11
+ class AttrDict #:nodoc:
12
+ include DateAttribute
13
+
14
+ def initialize(object, keys_and_values = {}, defs = {})
15
+ @object = object
16
+ @blob_dict = keys_and_values.symbolize_keys
17
+ @attr_defs = defs.symbolize_keys
18
+ @attr_cache = {}
19
+ end
20
+
21
+ def empty?
22
+ @blob_dict.empty? && @attr_defs.empty?
23
+ end
24
+
25
+ # Returns the title of the content - an HtmlString
26
+ def title
27
+ raw = @blob_dict[:title]
28
+ StringTagging.tag_as_html(raw, self)
29
+ end
30
+
31
+ def sort_keys #:nodoc:
32
+ [sort_key1, sort_key2, sort_key3]
33
+ end
34
+
35
+ # The body attribute may contain binary data for Obj.binary? or be an HtmlString.
36
+ def body
37
+ if object.binary?
38
+ binary_body.data if binary_body
39
+ else
40
+ StringTagging.tag_as_html(textual_body, self)
41
+ end
42
+ end
43
+
44
+ def body_length
45
+ if binary_body
46
+ binary_body.length
47
+ elsif body
48
+ body.length
49
+ else
50
+ 0
51
+ end
52
+ end
53
+
54
+ # Ensures the body data is in a file and returns its file system path.
55
+ def body_data_path
56
+ # TODO: Braucht/will jemand Text gestreamt?
57
+ return nil unless object.binary?
58
+ binary_body.path_of_stored_data(object.last_changed) if binary_body
59
+ end
60
+
61
+ def [](key)
62
+ send key
63
+ end
64
+
65
+ def permitted_groups
66
+ permission_live_server_read
67
+ rescue NoMethodError
68
+ []
69
+ end
70
+
71
+ def permissions
72
+ @permissions ||= OpenStruct.new({ :live => permitted_groups })
73
+ end
74
+
75
+ def respond_to?(method_id, include_private=false)
76
+ super || has_attribute(method_id)
77
+ end
78
+
79
+ private
80
+
81
+ def object
82
+ @object
83
+ end
84
+
85
+ def blob_dict
86
+ @blob_dict
87
+ end
88
+
89
+ def attr_defs
90
+ @attr_defs
91
+ end
92
+
93
+ def textual_body
94
+ return nil if object.binary?
95
+ blob_dict[:blob]
96
+ end
97
+
98
+ def binary_body
99
+ return nil unless object.binary? && has_attribute(:blob)
100
+ @binary_body ||= Blob.find(blob)
101
+ rescue RailsConnector::ResourceNotFound
102
+ nil
103
+ end
104
+
105
+ def method_missing(method_id, *args)
106
+ raise NoMethodError.new("missing method - #{method_id}") unless args.empty?
107
+ return attribute_value(method_id.to_s)
108
+ rescue UnknownAttributeError
109
+ raise NoMethodError.new("missing method - #{method_id}")
110
+ end
111
+
112
+ def attribute_value(key)
113
+ raise UnknownAttributeError.new unless has_attribute(key)
114
+
115
+ key = key.to_sym
116
+ raw = blob_dict[key]
117
+ @attr_cache[key] ||= case type_of(key)
118
+ when :markdown
119
+ StringTagging.tag_as_markdown(raw, self)
120
+ when :html
121
+ StringTagging.tag_as_html(raw, self)
122
+ when :date
123
+ DateAttribute.parse raw if raw
124
+ when :linklist
125
+ LinkList.new(raw)
126
+ else
127
+ raw
128
+ end
129
+ end
130
+
131
+ def has_attribute(key)
132
+ key = key.to_sym
133
+ !(blob_dict[key] || attr_defs[key]).nil?
134
+ end
135
+
136
+ def type_of(key)
137
+ key = key.to_sym
138
+ return :linklist if key == :text_links
139
+ attr_defs[key]['type'].to_sym rescue nil
140
+ end
141
+ end
142
+
143
+ class UnknownAttributeError < StandardError # :nodoc:
144
+ end
145
+
146
+ end
@@ -0,0 +1,11 @@
1
+ begin
2
+ require "rails_connector/attr_value_provider64"
3
+ rescue LoadError => e1
4
+ begin
5
+ require "rails_connector/attr_value_provider"
6
+ rescue LoadError => e2
7
+ raise "Could neither load attr_value_provider (#{e2.message}) " +
8
+ "nor attr_value_provider64 (#{e1.message})."
9
+ end
10
+ end
11
+
@@ -0,0 +1,67 @@
1
+ module RailsConnector
2
+
3
+ # A binary containing the blob data of a Content or CMS management
4
+ # data of Attribute (fields), ObjClass (object classes), for example.
5
+ class Blob < CmsBaseModel #:nodoc:
6
+ self.primary_key = "blob_name"
7
+
8
+ class << self
9
+ # Does not retrieve the column blob_data.
10
+ def find_with_excluded_blob_data(*args)
11
+ args << Hash.new unless Hash === args.last
12
+ args.last[:select] = 'blob_name, blob_length'
13
+ find_without_excluded_blob_data(*args)
14
+ end
15
+
16
+ alias_method_chain :find, :excluded_blob_data
17
+ end
18
+
19
+ def length
20
+ blob_length
21
+ end
22
+
23
+ def data
24
+ @data ||= self.class.find_without_excluded_blob_data(id).blob_data
25
+ end
26
+
27
+ def path_of_stored_data(use_cached_file_if_older_than = Time.now)
28
+ store_data if !File.exists?(path) || use_cached_file_if_older_than > File.mtime(path)
29
+ path
30
+ end
31
+
32
+ def self.cache_dir
33
+ File.join(Rails.root, %w(tmp cache))
34
+ end
35
+
36
+ def self.initialize_blob_streaming_for(adapter_name)
37
+ # Redefines store_data:
38
+ case adapter_name
39
+ when /mysql/i:
40
+ require "rails_connector/blob_mysql"
41
+ include BlobMysql
42
+ when /oracle/i:
43
+ require "rails_connector/blob_oracle"
44
+ include BlobOracle
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ def path
51
+ "#{Blob.cache_dir}/#{id}"
52
+ end
53
+
54
+ def store_data
55
+ store_data_atomically do |f|
56
+ f << data
57
+ end
58
+ end
59
+
60
+ def store_data_atomically(&block)
61
+ tmp_file = "#{path}.#{Process.pid}"
62
+ File.open(tmp_file, "w", &block)
63
+ File.rename(tmp_file, path)
64
+ end
65
+ end
66
+
67
+ end
@@ -0,0 +1,42 @@
1
+ require 'mysql'
2
+ require 'mysql_blob_streaming'
3
+
4
+ module RailsConnector
5
+
6
+ module BlobMysql #:nodoc:
7
+ def self.included(base)
8
+ base.instance_eval do
9
+ define_method :store_data do
10
+ store_data_atomically {|f| fetch_into_file(f)}
11
+ end
12
+ end
13
+ end
14
+
15
+ def fetch_into_file(file)
16
+ mysql = connection.instance_variable_get '@connection'
17
+ stmt = mysql.prepare("SELECT blob_data FROM #{self.class.table_name} WHERE #{self.class.primary_key}=?")
18
+
19
+ class << stmt
20
+ include MysqlBlobStreaming
21
+
22
+ attr_accessor :file
23
+ attr_accessor :logger
24
+
25
+ def handle_data(data)
26
+ @file << data
27
+ data = nil
28
+ GC.start
29
+ end
30
+ end
31
+
32
+ stmt.logger = ActiveRecord::Base.logger
33
+ stmt.logger.debug "Streaming blob from MySQL into #{file.path}"
34
+ stmt.file = file
35
+ stmt.execute(id)
36
+ stmt.stream(1.megabyte)
37
+ ensure
38
+ stmt.close if stmt
39
+ end
40
+ end
41
+
42
+ end
@@ -0,0 +1,39 @@
1
+ module RailsConnector
2
+
3
+ module BlobOracle #:nodoc:
4
+ def self.included(base)
5
+ base.instance_eval do
6
+ define_method :store_data do
7
+ store_data_atomically {|f| fetch_into_file(f)}
8
+ end
9
+ end
10
+ end
11
+
12
+ def fetch_into_file(file)
13
+ blob = blob_handle
14
+ while data = blob.read(buff_size ||= 1.megabyte)
15
+ file << data
16
+ data = nil
17
+ GC.start
18
+ end
19
+ ensure
20
+ blob.close if blob
21
+ end
22
+
23
+ private
24
+
25
+ def sql
26
+ %{
27
+ SELECT blob_data
28
+ FROM #{self.class.table_name}
29
+ WHERE #{self.class.primary_key} = :1
30
+ }
31
+ end
32
+
33
+ def blob_handle
34
+ con = connection.instance_variable_get '@connection'
35
+ con.exec(sql, id).fetch.first
36
+ end
37
+ end
38
+
39
+ end
@@ -0,0 +1,13 @@
1
+ module RailsConnector
2
+ class CacheMiddleware #:nodoc:
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ CmsBaseModel.cache do
9
+ @app.call(env)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,45 @@
1
+ module RailsConnector
2
+
3
+ # This is the abstract class from which all CMS models derive.
4
+ # The database connection is set to "infopark_rails_connector_${RAILS_ENV}" and the table prefix is
5
+ # determined from the instance_name.
6
+ #
7
+ # [instance_name] the name of the CMS instance
8
+ class CmsBaseModel < ActiveRecord::Base #:nodoc:
9
+ @@instance_name = 'default'
10
+ cattr_accessor :instance_name
11
+ self.abstract_class = true
12
+
13
+ def self.configure_database(db_connection_spec)
14
+ establish_connection(db_connection_spec)
15
+ Blob.initialize_blob_streaming_for(connection.adapter_name)
16
+ end
17
+
18
+ # Returns :table_name_with_underscore. All CMS models use this kind of primary key naming.
19
+ def self.primary_key_prefix_type
20
+ :table_name_with_underscore
21
+ end
22
+
23
+ # CmsBaseModel and all of its descendants are model classes that can access CMS contents.
24
+ def self.cms_model?
25
+ true
26
+ end
27
+
28
+ def readonly?
29
+ true
30
+ end
31
+
32
+ class << self
33
+ # The prefix of the table name is determined by using the instance name followed by an underscore.
34
+ # Set the instance_name in your environment to the CMS instance name (defaults to 'default').
35
+ #
36
+ # CmsBaseModel.instance_name = 'default'
37
+ #
38
+ # ActiveRecord::Base::primary_key_prefix_type has no effect here.
39
+ def table_name_prefix
40
+ @@instance_name.blank? ? '' : (@@instance_name + '_')
41
+ end
42
+ end
43
+ end
44
+
45
+ end
@@ -0,0 +1,28 @@
1
+ module RailsConnector
2
+
3
+ # Adds support for string columns which contain ISO dates
4
+ module DateAttribute #:nodoc: all
5
+ module ClassMethods
6
+ def date_attribute(*names)
7
+ names.each do |name|
8
+ module_eval %Q!
9
+ def #{name}
10
+ DateAttribute.parse(#{name}_before_type_cast) unless #{name}_before_type_cast.nil?
11
+ end
12
+ !
13
+ end
14
+ end
15
+ end
16
+
17
+ def self.included(base)
18
+ base.extend(ClassMethods)
19
+ end
20
+
21
+ def self.parse(iso_date_time)
22
+ Time.from_iso(iso_date_time).localtime
23
+ rescue ArgumentError
24
+ raise "The value is not a valid ISO date time: #{iso_date_time.inspect}"
25
+ end
26
+ end
27
+
28
+ end