Class: Fixation::FixtureTable
- Inherits:
-
Object
- Object
- Fixation::FixtureTable
- Defined in:
- lib/fixation/fixture_table.rb
Instance Attribute Summary collapse
-
#class_name ⇒ Object
readonly
Returns the value of attribute class_name.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#fixture_name ⇒ Object
readonly
Returns the value of attribute fixture_name.
-
#loaded_at ⇒ Object
readonly
Returns the value of attribute loaded_at.
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
Class Method Summary collapse
Instance Method Summary collapse
- #add_row(name, attributes) ⇒ Object
- #columns_hash ⇒ Object
- #embellish_fixture(name, attributes) ⇒ Object
- #embellished_rows ⇒ Object
- #fixture_ids ⇒ Object
-
#initialize(filename, basename, connection, loaded_at) ⇒ FixtureTable
constructor
A new instance of FixtureTable.
- #parsed_rows ⇒ Object
- #quote_value(column, value) ⇒ Object
- #statements ⇒ Object
Constructor Details
#initialize(filename, basename, connection, loaded_at) ⇒ FixtureTable
Returns a new instance of FixtureTable.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/fixation/fixture_table.rb', line 11 def initialize(filename, basename, connection, loaded_at) @filename = filename @connection = connection @loaded_at = loaded_at @fixture_name = basename.gsub('/', '_') @class_name = basename.classify begin @klass = @class_name.constantize @klass = nil unless @klass < ActiveRecord::Base rescue NameError ActiveRecord::Base.logger.warn "couldn't load #{class_name} for fixture table #{table_name}: #{$!}" end if @klass @table_name = @klass.table_name @primary_key = @klass.primary_key @record_timestamps = @klass. @inheritance_column = @klass.inheritance_column else @table_name = basename.gsub('/', '_') end end |
Instance Attribute Details
#class_name ⇒ Object (readonly)
Returns the value of attribute class_name.
3 4 5 |
# File 'lib/fixation/fixture_table.rb', line 3 def class_name @class_name end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
3 4 5 |
# File 'lib/fixation/fixture_table.rb', line 3 def connection @connection end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
3 4 5 |
# File 'lib/fixation/fixture_table.rb', line 3 def filename @filename end |
#fixture_name ⇒ Object (readonly)
Returns the value of attribute fixture_name.
3 4 5 |
# File 'lib/fixation/fixture_table.rb', line 3 def fixture_name @fixture_name end |
#loaded_at ⇒ Object (readonly)
Returns the value of attribute loaded_at.
3 4 5 |
# File 'lib/fixation/fixture_table.rb', line 3 def loaded_at @loaded_at end |
#table_name ⇒ Object (readonly)
Returns the value of attribute table_name.
3 4 5 |
# File 'lib/fixation/fixture_table.rb', line 3 def table_name @table_name end |
Class Method Details
.erb_content(filename) ⇒ Object
5 6 7 8 9 |
# File 'lib/fixation/fixture_table.rb', line 5 def self.erb_content(filename) template = File.read(filename) render_context = ActiveRecord::FixtureSet::RenderContext.create_subclass.new.get_binding ERB.new(template).result(render_context) end |
Instance Method Details
#add_row(name, attributes) ⇒ Object
121 122 123 124 |
# File 'lib/fixation/fixture_table.rb', line 121 def add_row(name, attributes) embellish_fixture(name, attributes) embellished_rows[name] = attributes end |
#columns_hash ⇒ Object
36 37 38 |
# File 'lib/fixation/fixture_table.rb', line 36 def columns_hash @columns_hash ||= connection.columns(table_name).index_by(&:name) end |
#embellish_fixture(name, attributes) ⇒ Object
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 |
# File 'lib/fixation/fixture_table.rb', line 61 def embellish_fixture(name, attributes) # populate the primary key column, if not already set if @primary_key && columns_hash[@primary_key] && !attributes.has_key?(@primary_key) attributes[@primary_key] = Fixation.identify(name, columns_hash[@primary_key].type) end # substitute $LABEL into all string values attributes.each do |column_name, value| attributes[column_name] = value.gsub("$LABEL", name) if value.is_a?(String) end # populate any timestamp columns, if not already set if @record_timestamps %w(created_at updated_at).each do |column_name| attributes[column_name] = loaded_at if columns_hash[column_name] && !attributes.has_key?(column_name) end %w(created_at updated_at).each do |column_name| attributes[column_name] = loaded_at.to_date if columns_hash[column_name] && !attributes.has_key?(column_name) end end # convert enum names to values @klass.defined_enums.each do |name, values| attributes[name] = values.fetch(attributes[name], attributes[name]) if attributes.has_key?(name) end if @klass.respond_to?(:defined_enums) # convert any association names into the identity column equivalent - following code from activerecord's fixtures.rb nonexistant_columns = attributes.keys - columns_hash.keys if @klass && nonexistant_columns.present? # If STI is used, find the correct subclass for association reflection reflection_class = if attributes.include?(@inheritance_column) attributes[@inheritance_column].constantize rescue @klass else @klass end nonexistant_columns.each do |column_name| association = reflection_class.reflect_on_association(column_name) if association.nil? raise ActiveRecord::Fixture::FormatError, "No column named #{column_name} found in table #{table_name}" elsif association.macro != :belongs_to raise ActiveRecord::Fixture::FormatError, "Association #{column_name} in table #{table_name} has type #{association.macro}, which is not currently supported" else value = attributes.delete(column_name) if association.[:polymorphic] && value.is_a?(String) && value.sub!(/\s*\(([^\)]*)\)\s*$/, "") # support polymorphic belongs_to as "label (Type)" attributes[association.foreign_type] = $1 end fk_name = (association.[:foreign_key] || "#{association.name}_id").to_s attributes[fk_name] = value ? ActiveRecord::FixtureSet.identify(value) : value end end end end |
#embellished_rows ⇒ Object
55 56 57 58 59 |
# File 'lib/fixation/fixture_table.rb', line 55 def embellished_rows @embellished_rows ||= parsed_rows.each do |name, attributes| embellish_fixture(name, attributes) end end |
#fixture_ids ⇒ Object
126 127 128 129 130 |
# File 'lib/fixation/fixture_table.rb', line 126 def fixture_ids embellished_rows.each_with_object({}) do |(name, attributes), ids| ids[name] = attributes['id'] || attributes['uuid'] end end |
#parsed_rows ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/fixation/fixture_table.rb', line 40 def parsed_rows result = YAML.load(self.class.erb_content(filename)) result ||= {} # for completely empty files unless (result.is_a?(Hash) || result.is_a?(YAML::Omap)) && result.all? { |name, attributes| name.is_a?(String) && attributes.is_a?(Hash) } raise ActiveRecord::Fixture::FormatError, "#{filename} needs to contain a hash of fixtures" end result.delete('DEFAULTS') result rescue ArgumentError, Psych::SyntaxError => error # we use exactly the same error class and message as ActiveRecord::FixtureSet in case anyone was depending on it raise ActiveRecord::Fixture::FormatError, "a YAML error occurred parsing #{filename}. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Please have a look at http://www.yaml.org/faq.html\nThe exact error was:\n #{error.class}: #{error}", error.backtrace end |
#quote_value(column, value) ⇒ Object
164 165 166 167 168 |
# File 'lib/fixation/fixture_table.rb', line 164 def quote_value(column, value) connection.quote(value) rescue TypeError connection.quote(YAML.dump(value)) end |
#statements ⇒ Object
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 |
# File 'lib/fixation/fixture_table.rb', line 132 def statements statements = ["DELETE FROM #{connection.quote_table_name table_name}"] unless embellished_rows.empty? # first figure out what columns we have to insert into; we're going to need to use the same names for # all rows so we can use the multi-line INSERT syntax columns_to_include = Set.new embellished_rows.each do |name, attributes| attributes.each do |column_name, value| raise ActiveRecord::Fixture::FormatError, "No column named #{column_name.inspect} found in table #{table_name.inspect} (attribute on fixture #{name.inspect})" unless columns_hash[column_name] columns_to_include.add(columns_hash[column_name]) end end # now build the INSERT statement quoted_column_names = columns_to_include.collect { |column| connection.quote_column_name(column.name) }.join(', ') statements << "INSERT INTO #{connection.quote_table_name table_name} (#{quoted_column_names}) VALUES " + embellished_rows.collect do |name, attributes| '(' + columns_to_include.collect do |column| if attributes.has_key?(column.name) quote_value(column, attributes[column.name]) else column.default_function || quote_value(column, column.default) end end.join(', ') + ')' end.join(', ') end statements end |