Class: FactoryGirl::RemoteStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/factory_girl_remote_strategy.rb

Direct Known Subclasses

RemoteNotFoundStrategy

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRemoteStrategy

Returns a new instance of RemoteStrategy.



18
19
20
# File 'lib/factory_girl_remote_strategy.rb', line 18

def initialize
  @strategy = FactoryGirl.strategy_by_name(:build).new
end

Class Method Details

.collection_url(collection, params = {}) ⇒ Object



66
67
68
# File 'lib/factory_girl_remote_strategy.rb', line 66

def collection_url(collection, params = {})
  (collection.first.is_a?(Class) ? collection.first : collection.first.class).instance_eval { "#{site}#{prefix}#{collection_name}.json#{'?' if params.any?}#{params.to_query}" }
end

.entity_hash(entity, params = {}) ⇒ Object

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/factory_girl_remote_strategy.rb', line 35

def entity_hash(entity, params = {})
  raise ArgumentError, "cann't construct hash for non ActiveResource::Base object" unless entity.is_a?(ActiveResource::Base)
  attributes = entity.attributes
  # Set belongs_to <association>_id instead of each <association>.
  attributes.select { |k, v| v.is_a?(ActiveResource::Base) }.each do |k, v|
    attributes[:"#{k}_id"] = v.id
    attributes.delete(k)
  end
  # Set has_many <association> instead of each <association(s)>_ids.
  attributes.map do |k, v|
    next unless k.to_s =~ /^(\w+)_ids$/ && FactoryGirl.factories.map(&:name).include?(f = $1.singularize.to_sym) && v.is_a?(Array)
    [k, v, f, $1.pluralize.to_sym]
  end.compact.each do |k, v, f, r|
    attributes[r] = v.map { |id| FactoryGirl.remote(f, id: id) }
    attributes.delete(k)
  end
  # Serilaize has_many associations.
  attributes.each do |k, v|
    if v.is_a?(Array) && v.first.is_a?(ActiveResource::Base)
      attributes[k] = v.map { |e| entity_hash(e) }
    end
  end
  { entity.class.element_name => attributes }.tap do |h|
    h[:_metadata] = { abilities: %w(update destroy) } unless params[:metadata] == false
  end
end

.entity_url(entity) ⇒ Object



62
63
64
# File 'lib/factory_girl_remote_strategy.rb', line 62

def entity_url(entity)
  "#{entity.class.site}#{entity.class.prefix}#{entity.class.collection_name}/#{entity.id}.json"
end

.register_request(http_method, uri, options) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/factory_girl_remote_strategy.rb', line 79

def register_request(http_method, uri, options)
  if @@library == :fakeweb
    FakeWeb.register_uri(http_method, uri, options)
  else
    WebMock.stub_request(http_method, uri).to_return(options)
  end
end

.stub_requests_with(library) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/factory_girl_remote_strategy.rb', line 70

def stub_requests_with(library)
  library = library.to_s
  raise ArgumentError, "Unknown library '#{library}', please try :webmock or :fakeweb" unless %w(webmock fakeweb).include?(library)
  require library
  @@library = library.to_sym
rescue LoadError
  worn "WARNING: gem '#{library}' is not installed"
end

Instance Method Details

#result(evaluation) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/factory_girl_remote_strategy.rb', line 24

def result(evaluation)
  @strategy.result(evaluation).tap do |e|
    self.class.register_request(:get, self.class.entity_url(e), body: self.class.entity_hash(e).to_json)
    self.class.register_request(:put, self.class.entity_url(e), body: self.class.entity_hash(e).to_json)
    remote_search(e, search: { :"#{e.class.primary_key}_eq" => e.public_send(e.class.primary_key) })
    remote_search(e, search: { :"#{e.class.primary_key}_in" => [e.public_send(e.class.primary_key)] })
    evaluation.notify(:after_remote, e) # runs after(:remote) callback
  end
end