Class: Binda::Structure

Inherits:
ApplicationRecord show all
Extended by:
FriendlyId
Defined in:
app/models/binda/structure.rb

Instance Method Summary collapse

Instance Method Details

#add_default_boardObject

Add default board to a structure if needed



81
82
83
84
85
86
87
88
# File 'app/models/binda/structure.rb', line 81

def add_default_board
  if Board.where(structure_id: self.id).empty?
    board = self.build_board( name: self.name )
    unless board.save
      return redirect_to structure_path(self.slug), flash: { error: I18n.t('binda.default_field_group.error_on_create') }
    end
  end
end

#add_default_field_groupredirect

Add a field group as a default

In order to speed up the setup process Binda will automatically provide a

field group called 'General Details' after the structure is created.

Returns:

  • (redirect)


59
60
61
62
63
64
65
66
# File 'app/models/binda/structure.rb', line 59

def add_default_field_group
  # Creates a default empty field group 
  field_group = self.field_groups.build(name: I18n.t('binda.default_field_group.name'), position: 1)
  # Unless there is a problem...
  unless field_group.save
    return redirect_to structure_path(self.slug), flash: { error: I18n.t('binda.default_field_group.error_on_create') }
  end
end

#add_instance_detailsObject

Add details based on instance type

If instance_type is set to ‘setting’ it generates a default Binda::Setting instance after creation.

The generated instance will be associated to the structure and named after it.
It also disable categories (this could be a different method, or method could be more explicit)


73
74
75
76
77
78
# File 'app/models/binda/structure.rb', line 73

def add_instance_details
  if self.instance_type == 'board'
    self.update_attribute('has_categories', false)
      add_default_board
  end
end

#default_slugObject

Set slug name

It generates 4 possible slugs before falling back to FriendlyId default behaviour



47
48
49
50
51
52
# File 'app/models/binda/structure.rb', line 47

def default_slug
  [ "#{ self.name.parameterize }",
    "#{ self.name.parameterize }-1",
    "#{ self.name.parameterize }-2",
    "#{ self.name.parameterize }-3" ]
end

#is_rejected(attributes) ⇒ Object

Sets the validation rules to accept and save an attribute



40
41
42
# File 'app/models/binda/structure.rb', line 40

def is_rejected(attributes)
  attributes['name'].blank?
end

#set_default_positionobject

Set default position after create

This methods ensure that every repeater instance has an explicit position.

The latest repeater created gets the highest position number.

Returns:

  • (object)

    Repeater instance



96
97
98
99
# File 'app/models/binda/structure.rb', line 96

def set_default_position
  position = Structure.all.length
  self.update_attribute('position', position)
end

#should_generate_new_friendly_id?Boolean

Friendly id preference on slug generation

Method inherited from friendly id



34
35
36
# File 'app/models/binda/structure.rb', line 34

def should_generate_new_friendly_id?
  slug.blank?
end