Skip to content

Commit

Permalink
Use the already included module instead of reopening classes.
Browse files Browse the repository at this point in the history
Related to ManageIQ#14128

We should either use RssFeed.class_eval to reopen the class to force
rails to load the RssFeed model first or the technique in this commit,
which is to just include the module in RssFeed.  Note, this module was
already being included so there was no reason to reopen the class also.

The existing layout of re-opening the RssFeed with different class
definitions:

rss_feed/import_export.rb:
class RssFeed

rss_feed.rb:
class RssFeed < ApplicationRecord

can cause `TypeError: superclass mismatch for class RssFeed` if
the import_export.rb is loaded first.
  • Loading branch information
jrafanie committed Mar 3, 2017
1 parent cb1224a commit 0229960
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
3 changes: 2 additions & 1 deletion app/models/rss_feed.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
require 'resource_feeder/common'
class RssFeed < ApplicationRecord
include ResourceFeeder
include_concern 'ImportExport'

validates_presence_of :name
validates_uniqueness_of :name

attr_accessor :options

acts_as_miq_taggable
include_concern 'ImportExport'

YML_DIR = File.join(File.expand_path(Rails.root), "product", "alerts", "rss")

Expand Down
14 changes: 6 additions & 8 deletions app/models/rss_feed/import_export.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
class RssFeed
module ImportExport
extend ActiveSupport::Concern
module RssFeed::ImportExport
extend ActiveSupport::Concern

def export_to_array
h = attributes
["id", "created_on", "updated_on", "yml_file_mtime"].each { |k| h.delete(k) }
[self.class.to_s => h]
end
def export_to_array
h = attributes
["id", "created_on", "updated_on", "yml_file_mtime"].each { |k| h.delete(k) }
[self.class.to_s => h]
end
end
5 changes: 5 additions & 0 deletions spec/factories/rss_feed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryGirl.define do
factory :rss_feed do
sequence(:name) { |n| "feed_#{seq_padded_for_sorting(n)}" }
end
end
6 changes: 6 additions & 0 deletions spec/models/rss_feed/import_export_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
describe RssFeed::ImportExport do
it "#export_to_array" do
expect(FactoryGirl.create(:rss_feed, :title => "Latest things!!!")
.export_to_array.first["RssFeed"]["title"]).to eq("Latest things!!!")
end
end

0 comments on commit 0229960

Please sign in to comment.