Module: Spider::Model::ActiveRecordModel::ClassMethods

Defined in:
lib/spiderfw/model/active_record.rb

Instance Method Summary collapse

Instance Method Details

#arObject



18
19
20
# File 'lib/spiderfw/model/active_record.rb', line 18

def ar
    @ar
end

#ar=(val) ⇒ Object



14
15
16
# File 'lib/spiderfw/model/active_record.rb', line 14

def ar=(val)
    @ar = val
end

#ar_definedObject



30
31
32
# File 'lib/spiderfw/model/active_record.rb', line 30

def ar_defined
    @ar_defined
end

#ar_defined=(val) ⇒ Object



26
27
28
# File 'lib/spiderfw/model/active_record.rb', line 26

def ar_defined=(val)
    @ar_defined = val
end

#ar_schemaObject



22
23
24
# File 'lib/spiderfw/model/active_record.rb', line 22

def ar_schema
    @ar_schema
end

#ar_through_modelsObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/spiderfw/model/active_record.rb', line 34

def ar_through_models
    ar = @ar
    m = []
    ar.reflections.each do |name, reflection|
         if (reflection.through_reflection)
            m << reflection.through_reflection.klass.spider_model
        end
    end
    return m
end

#define_from_arObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/spiderfw/model/active_record.rb', line 60

def define_from_ar
    ar = @ar
    type_conversion = {
        :integer       => Fixnum,
        :float         => Float,
        :decimal       => BigDecimal,
        :datetime      => DateTime,
        :date          => Date,
        :timestamp     => DateTime,
        :time          => DateTime,
        :string        => String,
        :text          => Spider::DataTypes::Text,
        :binary        => Spider::DataTypes::Binary,
        :boolean       => Spider::DataTypes::Bool
    }
    skip_columns = {}
    ar.reflections.each do |name, refl|
        begin
            skip_columns[refl.association_foreign_key.to_sym] = true
            skip_columns[refl.primary_key_name.to_sym] = true
        rescue NoMethodError
        end
    end
    tree_options = nil
    if (ar.respond_to?(:acts_as_nested_set_options))
        tree_options = ar.acts_as_nested_set_options
        skip_columns[tree_options[:parent_column].to_sym] = true
        skip_columns[tree_options[:left_column].to_sym] = true
        skip_columns[tree_options[:right_column].to_sym] = true
    end
    @ar_schema = {:table => ar.table_name, :columns => {}}
    ar.columns.each do |col|
        options = {}
        name = col.name.to_sym
        name = :obj_created if name == :created_on
        name = :obj_modified if name == :updated_on
        column = col.name
        type = type_conversion[col.type]
        skip = skip_columns[name]
        if ar.primary_key == column
            options[:primary_key] = true 
            options[:autoincrement] = true
        end
        
        hidden = [:obj_created, :obj_modified]
        options[:hidden] = true if hidden.include?(name)
        next if skip
#                    debugger if name == :parent_id
        @ar_schema[:columns][name] = col.name
        next unless type
        self.element(name, type, options)
    end
    ar.reflections.each do |name, reflection|
        options = {}
        association = nil
        if (reflection.macro == :belongs_to)
            association = nil
        else
            association = :many
            options[:multiple] = true
                
            # unless options[:reverse]
            #     options[:add_reverse] = ar.send(:undecorated_table_name, ar.name)
            # end
            options[:has_single_reverse] = true
        end
        options[:association] = association
        if (reflection.options[:polymorphic])
            self.element(name, Fixnum, options)
            @ar_schema[:columns][name] = reflection.primary_key_name.to_s
            next
        end
        begin
            if (reflection.through_reflection)
                klass = reflection.through_reflection.klass

                options[:junction] = true
                klass.reflections.each do |r_name, r_refl|
                    if (r_refl.klass == ar && r_refl.primary_key_name == reflection.primary_key_name)
                        options[:junction_their_element] = r_name
                    elsif(r_refl.klass == reflection.klass && r_refl.primary_key_name == reflection.association_foreign_key)
                        options[:junction_our_element] = r_name
                    end
                end
                assoc_type = klass.spider_model
#                            assoc_type.integrate(options[:junction_their_element])
                options[:association_type] = assoc_type
                type = reflection.klass.spider_model
                options[:junction_id] = :id
                options.delete(:has_single_reverse)
            elsif (reflection.options[:join_table])
                klass = reflection.klass
                type = klass.spider_model
                junction_model_name = Spider::Inflector.camelize([ar.name.to_s, klass.name.to_s].sort.join('_')).to_sym
                pm = self.parent_module
                self_name = ar.name.downcase.to_sym
                other_name = klass.name.downcase.to_sym
                options[:junction_their_element] = other_name
                options[:junction_our_element] = self_name
                options[:junction] = true
                options[:reverse] = self_name
                if (pm.const_defined?(junction_model_name))
                    junction_mod = pm.const_get(junction_model_name)
                else
                    junction_mod = Class.new(Spider::Model::BaseModel)
                    pm.const_set(junction_model_name, junction_mod)
                    
                    junction_mod.attributes[:sub_model] = ar.spider_model
                    junction_mod.element(self_name, ar.spider_model, :hidden => true, :reverse => name, :primary_key => true) 
                    junction_mod.element(other_name, type, :primary_key => true)
                    junction_mod.ar_defined = true
                end
                unless junction_mod.is_a?(ActiveRecordModel)
                    junction_mod.instance_eval do
                        include ActiveRecordModel
                    end
                end
                junction_mod.instance_variable_set("@ar_schema", {
                    :table => reflection.options[:join_table],
                    :columns => {
                        self_name => "#{ar.name.downcase}_id",
                        other_name => "#{klass.name.downcase}_id"
                    }
                })
                options[:through] = junction_mod
                options[:association_type] = junction_mod
                options[:junction_id] = :id
                options.delete(:has_single_reverse)
                #junction_mod.integrate(other_name, :hidden => true, :no_pks => true)
            else
                klass = reflection.klass
                type = klass.spider_model
            end
            klass.reflections.each do |r_name, r_refl|
                begin
                    if (r_refl.klass == ar && r_refl.primary_key_name == reflection.primary_key_name)
                        options[:reverse] = r_name
                    end
                rescue => exc
                end
            end                            
            type ||= klass.spider_model
        rescue NameError => exc
            #$stderr << exc.inspect+"\n"
            next
        end

        next unless type
        next if options[:junction]  && (!options[:junction_their_element] || !options[:junction_our_element])
        self.element(name, type, options)
        unless reflection.options[:join_table]
            if (reflection.table_name == ar.table_name)
                @ar_schema[:columns][name] = reflection.association_foreign_key
            elsif (!(reflection.macro == :has_many || reflection.through_reflection))
                @ar_schema[:columns][name] = reflection.primary_key_name.to_s
            end
        end
    end
    if (tree_options)
        include Spider::Model::Tree
        self.tree :children, :tree_left => :lft, :tree_right => :rgt, :reverse => :parent, :tree_depth => :depth
        @ar_schema[:columns].merge!({
            :lft => tree_options[:left_column],
            :rgt => tree_options[:right_column],
            :parent => tree_options[:parent_column]
        })
    end

    
    def get_connection_url(conf)
        url = case conf['adapter']
        when 'mysql'
            str = "db:mysql://#{conf['username']}:#{conf['password']}@#{conf['host']}"
            str += conf['port'] if conf['port']
            str += "/#{conf['database']}"
            str
        end
        return url
    end
    
    if (@rails_app_name)
        rails_app_name = @rails_app_name
    elsif (const_defined?("SETTINGS") && SETTINGS.is_a?(Hash))
        rails_app_name = SETTINGS[:app_name]
    elsif const_defined?("APP_NAME")
        rails_app_name = APP_NAME
    else
        rails_app_name = 'rails'
    end
    Spider.conf.set("storages.#{rails_app_name}.url", get_connection_url(ar.configurations['production']))
    self.use_storage(rails_app_name)
    
    
end

#get_connection_url(conf) ⇒ Object



229
230
231
232
233
234
235
236
237
238
# File 'lib/spiderfw/model/active_record.rb', line 229

def get_connection_url(conf)
    url = case conf['adapter']
    when 'mysql'
        str = "db:mysql://#{conf['username']}:#{conf['password']}@#{conf['host']}"
        str += conf['port'] if conf['port']
        str += "/#{conf['database']}"
        str
    end
    return url
end

#prepare_to_codeObject



53
54
55
56
57
58
# File 'lib/spiderfw/model/active_record.rb', line 53

def prepare_to_code
    c = super
    schema = "@ar_schema = #{@ar_schema.inspect}\n"
    c[:additional_code] << schema
    return c
end

#rails_app_nameObject



49
50
51
# File 'lib/spiderfw/model/active_record.rb', line 49

def rails_app_name
    @rails_app_name
end

#rails_app_name=(val) ⇒ Object



45
46
47
# File 'lib/spiderfw/model/active_record.rb', line 45

def rails_app_name=(val)
    @rails_app_name = val
end