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



85
86
87
88
89
90
91
92
93
# File 'app/models/binda/structure.rb', line 85

def add_default_board
	if Board.where(structure_id: self.id).empty?
		board = self.build_board( name: self.name )
		unless board.save
			errors.add(:base, I18n.t('binda.default_field_group.error_on_create'))
			# 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)


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

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)


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

def add_instance_details
	if self.instance_type == 'board'
		add_default_board
	end
end

#board_contraintsObject



80
81
82
# File 'app/models/binda/structure.rb', line 80

def board_contraints
	self.has_categories = false if self.instance_type == 'board'
end

#default_slugObject

Set slug name

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



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

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



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

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



101
102
103
# File 'app/models/binda/structure.rb', line 101

def set_default_position
    Structure.all.each{|structure| structure.increment(:position).save!}
end

#should_generate_new_friendly_id?Boolean

Friendly id preference on slug generation

Method inherited from friendly id



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

def should_generate_new_friendly_id?
	slug.blank?
end