Class: Adva::Static::Import::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/adva/static/import/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, record, attributes) ⇒ Request

Returns a new instance of Request.



7
8
9
10
11
# File 'lib/adva/static/import/request.rb', line 7

def initialize(source, record, attributes)
  @source = source
  @record = record
  @attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/adva/static/import/request.rb', line 5

def attributes
  @attributes
end

#recordObject (readonly)

Returns the value of attribute record.



5
6
7
# File 'lib/adva/static/import/request.rb', line 5

def record
  @record
end

#sourceObject (readonly)

Returns the value of attribute source.



5
6
7
# File 'lib/adva/static/import/request.rb', line 5

def source
  @source
end

Instance Method Details

#controllerObject



46
47
48
49
50
51
# File 'lib/adva/static/import/request.rb', line 46

def controller
  @controller ||= controller_name.constantize.new.tap do |controller|
    controller.request = ActionDispatch::TestRequest.new
    controller.params = params_for(controller)
  end
end

#create?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/adva/static/import/request.rb', line 34

def create?
  !update? && !destroy?
end

#destroy?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/adva/static/import/request.rb', line 42

def destroy?
  record.persisted? && !source.exist?
end

#paramsObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/adva/static/import/request.rb', line 13

def params
  @params ||= begin
    key = model_name.underscore.to_sym
    if destroy?
      params = { '_method' => 'delete', key => { :id => record.id } }
    else
      params = { model_name.underscore.to_sym => attributes }
      params.merge!('_method' => 'put') if update?
    end
    stringify(params)
  end
end

#pathObject



26
27
28
# File 'lib/adva/static/import/request.rb', line 26

def path
  controller.polymorphic_path(controller.resources)
end

#public_pathObject



30
31
32
# File 'lib/adva/static/import/request.rb', line 30

def public_path
  controller.public_url_for(controller.resources, :routing_type => :path)
end

#update?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/adva/static/import/request.rb', line 38

def update?
  record.persisted? && source.exist?
end