Class: AwesomeTranslations::Group

Inherits:
Object
  • Object
show all
Defined in:
app/models/awesome_translations/group.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Group

Returns a new instance of Group.



12
13
14
15
16
17
# File 'app/models/awesome_translations/group.rb', line 12

def initialize(args)
  @handler = args.fetch(:handler)
  @id = args.fetch(:id)
  @data = args[:data] || {}
  raise "Invalid ID: #{@id}" if @id.blank?
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



2
3
4
# File 'app/models/awesome_translations/group.rb', line 2

def data
  @data
end

#handlerObject (readonly)

Returns the value of attribute handler.



2
3
4
# File 'app/models/awesome_translations/group.rb', line 2

def handler
  @handler
end

#idObject (readonly)

Returns the value of attribute id.



2
3
4
# File 'app/models/awesome_translations/group.rb', line 2

def id
  @id
end

Class Method Details

.find_by(handler:, id:) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


4
5
6
7
8
9
10
# File 'app/models/awesome_translations/group.rb', line 4

def self.find_by(handler:, id:)
  handler.groups.each do |group|
    return group if group.id == id.to_s
  end

  raise ActiveRecord::RecordNotFound, "Group not found by handler and ID: '#{handler.name}', '#{id}'."
end

Instance Method Details

#inspectObject



47
48
49
# File 'app/models/awesome_translations/group.rb', line 47

def inspect
  to_s
end

#nameObject



39
40
41
# File 'app/models/awesome_translations/group.rb', line 39

def name
  @data[:name].presence || id.presence
end

#to_paramObject



35
36
37
# File 'app/models/awesome_translations/group.rb', line 35

def to_param
  id
end

#to_sObject



43
44
45
# File 'app/models/awesome_translations/group.rb', line 43

def to_s
  "<AwesomeTranslations::Group id=\"#{@id}\" name=\"#{name}\">"
end

#translations(args = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/awesome_translations/group.rb', line 19

def translations(args = {})
  translations_list = @handler.translations_for_group(self)

  args.each do |key, value|
    if key == :finished
      translations_list = translations_list.select(&:finished?) if value
    elsif key == :unfinished
      translations_list = translations_list.select(&:unfinished?) if value
    else
      raise "Unknown key: #{key}"
    end
  end

  translations_list
end