Class: Wicked::Pipeline::BaseStep

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::AttributeAssignment, ActiveModel::Attributes, ActiveModel::Validations
Defined in:
lib/wicked/pipeline/base_step.rb

Direct Known Subclasses

ReadonlyStep

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, params = nil) ⇒ BaseStep

Returns a new instance of BaseStep.

Parameters:

  • resource (ActiveRecord::Base)

    The record associated with the step

  • params (Hash, ActionController::Parameters) (defaults to: nil)

    Attribute changes to be applied to the resource



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/wicked/pipeline/base_step.rb', line 17

def initialize(resource, params = nil)
  super()

  @resource = resource

  attributes.each_key do |key|
    public_send("#{key}=", resource.public_send(key)) if resource.respond_to?(key)
  end

  unless params.nil?
    parameters = _permit_params!(params)
    self.attributes = parameters.select { |key, _| respond_to?("#{key}=") }
    resource.attributes = parameters.select { |key, _| resource.respond_to?("#{key}=") }
  end

  regenerate_blocking_reasons!
end

Instance Attribute Details

#resourceActiveRecord::Base (readonly)

Returns The record associated with the step.

Returns:

  • (ActiveRecord::Base)

    The record associated with the step



13
14
15
# File 'lib/wicked/pipeline/base_step.rb', line 13

def resource
  @resource
end

Class Method Details

.==(other) ⇒ Boolean

Check whether two steps are the same

It returns true only if other is the same step class then self or if self.to_s is equal to other. other.step_name.

Parameters:

  • other (BaseStep, String, Symbol)

    the other step tested for equality

Returns:

  • (Boolean)

See Also:



191
192
193
# File 'lib/wicked/pipeline/base_step.rb', line 191

def self.==(other)
  super || to_s == other.to_s
end

.===(other) ⇒ Boolean

Check whether two steps are the same

It returns true only if other is the same step class then self or if self.to_s is equal to other. other.step_name.

Parameters:

  • other (BaseStep, String, Symbol)

    the other step tested for equality

Returns:

  • (Boolean)

See Also:



207
208
209
# File 'lib/wicked/pipeline/base_step.rb', line 207

def self.===(other)
  equal?(other)
end

.eql?(other) ⇒ Boolean

Check whether two steps are the same

It returns true only if other is the same step class then self or if self.to_s is equal to other. other.step_name.

Parameters:

  • other (BaseStep, String, Symbol)

    the other step tested for equality

Returns:

  • (Boolean)

See Also:



196
197
198
# File 'lib/wicked/pipeline/base_step.rb', line 196

def self.eql?(other)
  self == other
end

.equal?(other) ⇒ Boolean

Check whether two steps are the same

It returns true only if other is the same step class then self or if self.to_s is equal to other. other.step_name.

Parameters:

  • other (BaseStep, String, Symbol)

    the other step tested for equality

Returns:

  • (Boolean)

See Also:



202
203
204
# File 'lib/wicked/pipeline/base_step.rb', line 202

def self.equal?(other)
  self == other
end

.i18n_scopeObject



259
260
261
# File 'lib/wicked/pipeline/base_step.rb', line 259

def self.i18n_scope
  @_i18n_scope ||= name.underscore.tr("/", ".").prepend("steps.")
end

.step_nameString

Returns the step name of the step object

Examples:

Step object without a namespace

FinancialSituationnStep.step_name #=> "financial_situation"

Nested step object

Users::FinancialSituationnStep.step_name #=> "financial_situation"

Returns:

  • (String)


148
149
150
# File 'lib/wicked/pipeline/base_step.rb', line 148

def self.step_name
  @_step_name ||= to_s.gsub(%r{(?:\w+/)*}, "")
end

.to_sString

Returns the String representation of the step object

Examples:

Step object without a namespace

FinancialSituationnStep.to_s #=> "financial_situation"

Nested step object

Users::FinancialSituationnStep.to_s #=> "users/financial_situation"

Returns:

  • (String)


135
136
137
# File 'lib/wicked/pipeline/base_step.rb', line 135

def self.to_s
  @_to_s ||= name.to_s.underscore.sub(/_step\z/, "")
end

Instance Method Details

#===(other) ⇒ Boolean

Check whether two steps are the same

It returns true only if other is the same step class then self or if self.to_s is equal to other. other.step_name.

Parameters:

  • other (BaseStep, String, Symbol)

    the other step tested for equality

Returns:

  • (Boolean)

See Also:



217
218
219
# File 'lib/wicked/pipeline/base_step.rb', line 217

def ===(other)
  equal?(other)
end

#blocking?Boolean

Checks if the step is blocking subsequent steps in a pipeline. In other words, if a step is “blocking”, all the subsequent steps in the pipeline will be marked as inaccessible.

This can be used to short-circuit a pipeline.

Returns:

  • (Boolean)


89
90
91
# File 'lib/wicked/pipeline/base_step.rb', line 89

def blocking?
  false
end

#blocking_reasonString?

Returns the reason why a step is blocking, or nil if it is not blocking.

This should be used to set an overall blocking reason. To set per-attribute blocking reasons use #blocking_reasons.

Returns:

  • (String, nil)

See Also:



108
109
110
# File 'lib/wicked/pipeline/base_step.rb', line 108

def blocking_reason
  nil
end

#blocking_reasonsHash{String,Symbol => Array<String>}

Note:

This method should not be overridden, it should just be used to access blocking reasons. To add a new reason, just add it to the hash. To reset all the reasons, override the #regenerate_blocking_reasons! method in subsclasses.

Note:

Warning: Every time #valid? is called the blocking reasons will be regenereted.

Returns reasons for each attribute why a step is blocking.

Returns:

  • (Hash{String,Symbol => Array<String>})

    hash of reasons for each attribute.

See Also:



122
123
124
# File 'lib/wicked/pipeline/base_step.rb', line 122

def blocking_reasons
  @blocking_reasons ||= ActiveSupport::HashWithIndifferentAccess.new { |h, k| h[k] = [] }
end

#equal?(other) ⇒ Boolean

Check whether two steps are the same

It returns true only if other is the same step class then self or if self.to_s is equal to other. other.step_name.

Parameters:

  • other (BaseStep, String, Symbol)

    the other step tested for equality

Returns:

  • (Boolean)

See Also:



212
213
214
# File 'lib/wicked/pipeline/base_step.rb', line 212

def equal?(other)
  self.class.equal?(other)
end

#idNumber, Symbol

Returns The ID of the resource or :new if the the resource is a new record.

Returns:

  • (Number, Symbol)

    The ID of the resource or :new if the the resource is a new record



222
223
224
# File 'lib/wicked/pipeline/base_step.rb', line 222

def id
  resource.id || :new
end

#persisted?Boolean

Checks if the resource exists in the database

Always returns true if the step is readonly

Returns:

  • (Boolean)


69
70
71
# File 'lib/wicked/pipeline/base_step.rb', line 69

def persisted?
  readonly? || resource.persisted?
end

#readonly?Boolean

Check if the step is readonly

Returns:

  • (Boolean)


96
97
98
# File 'lib/wicked/pipeline/base_step.rb', line 96

def readonly?
  false
end

#save(**options, &block) ⇒ Boolean

Saves the resource

If the step is readonly the record will not be saved and the method will return true

Returns:

  • (Boolean)


41
42
43
44
45
46
47
# File 'lib/wicked/pipeline/base_step.rb', line 41

def save(**options, &block)
  return true if readonly?

  cleanup_stale_data

  valid? && resource.save(**options, &block)
end

#saved?Boolean

Checks that the step has been successfully saved and there are no unsaved changes

Always returns true if the step is readonly

Returns:

  • (Boolean)


78
79
80
# File 'lib/wicked/pipeline/base_step.rb', line 78

def saved?
  readonly? || (persisted? && resource.saved_changes? && !resource.has_changes_to_save?)
end

#step_nameString

Returns the step name of the step object

Examples:

Step object without a namespace

FinancialSituationnStep.new(resource).step_name #=> "financial_situation"

Nested step object

Users::FinancialSituationnStep.new(resource).step_name #=> "financial_situation"

Returns:

  • (String)

See Also:



178
179
180
# File 'lib/wicked/pipeline/base_step.rb', line 178

def step_name
  self.class.step_name
end

#to_sString

Returns the String representation of the step object

Examples:

Step object without a namespace

FinancialSituationnStep.new(resource).to_s #=> "financial_situation"

Nested step object

Users::FinancialSituationnStep.new(resource).to_s #=> "users/financial_situation"

Returns:

  • (String)

See Also:



163
164
165
# File 'lib/wicked/pipeline/base_step.rb', line 163

def to_s
  self.class.to_s
end

#valid?Boolean

Check if both the step and the resource are valid

Always returns true if the step is readonly

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
# File 'lib/wicked/pipeline/base_step.rb', line 54

def valid?
  return true if readonly?

  is_valid = super
  resource.validate
  resource.errors.merge!(errors)
  regenerate_blocking_reasons!
  is_valid && resource.errors.none?
end