Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Default Fields and Attributes into a Template

Class methods were feeling a little ugly - this is more code, but cleaner, I think. And perhaps an approach that could be used elsewhere.
  • Loading branch information...
commit 31a5df5babe1b185e54f78d7a5915ec8d9c0d3b8 1 parent 665adb4
Pat Allan pat authored
39 lib/thinking_sphinx/active_record/sql_source.rb
View
@@ -2,37 +2,6 @@ class ThinkingSphinx::ActiveRecord::SQLSource < Riddle::Configuration::SQLSource
attr_reader :model, :database_settings, :options, :conditions, :groupings
attr_accessor :fields, :attributes, :associations
- def self.internal_attributes(model)
- [
- ThinkingSphinx::ActiveRecord::Attribute.new(
- ThinkingSphinx::ActiveRecord::Column.new(:id),
- :as => :sphinx_internal_id, :type => :integer
- ),
- ThinkingSphinx::ActiveRecord::Attribute.new(
- ThinkingSphinx::ActiveRecord::Column.new("'#{model.name}'"),
- :as => :sphinx_internal_class, :type => :string
- ),
- ThinkingSphinx::ActiveRecord::Attribute.new(
- ThinkingSphinx::ActiveRecord::Column.new("0"),
- :as => :sphinx_deleted, :type => :boolean
- )
- ]
- end
-
- def self.internal_fields(model)
- column = "'#{model.name}'"
- if model.column_names.include?(model.inheritance_column)
- column = model.inheritance_column.to_sym
- end
-
- [
- ThinkingSphinx::ActiveRecord::Field.new(
- ThinkingSphinx::ActiveRecord::Column.new(column),
- :as => :sphinx_class
- )
- ]
- end
-
# Options:
# - :name
# - :offset
@@ -47,12 +16,14 @@ def initialize(model, options = {})
@database_settings = model.connection.instance_variable_get(:@config).clone
@options = options
- @fields = self.class.internal_fields(model)
- @attributes = self.class.internal_attributes(model)
+ @fields = []
+ @attributes = []
@associations = []
@conditions = []
@groupings = []
+ Template.new(self).apply
+
name = "#{options[:name] || model.name.downcase}_#{name_suffix}"
super name, type
@@ -158,3 +129,5 @@ def prepare_for_render
@prepared = true
end
end
+
+require 'thinking_sphinx/active_record/sql_source/template'
42 lib/thinking_sphinx/active_record/sql_source/template.rb
View
@@ -0,0 +1,42 @@
+class ThinkingSphinx::ActiveRecord::SQLSource::Template
+ attr_reader :source
+
+ def initialize(source)
+ @source = source
+ end
+
+ def apply
+ add_field class_column, :sphinx_class
+
+ add_attribute :id, :sphinx_internal_id, :integer
+ add_attribute class_column, :sphinx_internal_class, :string
+ add_attribute '0', :sphinx_deleted, :boolean
+ end
+
+ private
+
+ def add_attribute(column, name, type)
+ source.attributes << ThinkingSphinx::ActiveRecord::Attribute.new(
+ ThinkingSphinx::ActiveRecord::Column.new(column),
+ :as => name, :type => type
+ )
+ end
+
+ def add_field(column, name)
+ source.fields << ThinkingSphinx::ActiveRecord::Field.new(
+ ThinkingSphinx::ActiveRecord::Column.new(column), :as => name
+ )
+ end
+
+ def class_column
+ inheritance_column? ? model.inheritance_column.to_sym : "'#{model.name}'"
+ end
+
+ def inheritance_column?
+ model.column_names.include?(model.inheritance_column)
+ end
+
+ def model
+ source.model
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.