Class: Course::Settings::Sidebar

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Conversion, ActiveModel::Model
Defined in:
app/models/course/settings/sidebar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(course_settings, sidebar_items) ⇒ Sidebar

Returns a new instance of Sidebar

Parameters:

  • course_settings (#settings)

    The settings object provided by the settings_on_rails gem.

  • sidebar_items (Array<Hash>)

    The sidebar items.


10
11
12
13
14
15
16
17
18
# File 'app/models/course/settings/sidebar.rb', line 10

def initialize(course_settings, sidebar_items)
  @settings = course_settings.settings(:sidebar)
  @sidebar_items = begin
    sidebar_items = sidebar_items.map do |item|
      Course::Settings::SidebarItem.new(@settings, item)
    end
    sidebar_items.sort_by(&:weight)
  end
end

Instance Attribute Details

Returns the value of attribute sidebar_items


6
7
8
# File 'app/models/course/settings/sidebar.rb', line 6

def sidebar_items
  @sidebar_items
end

Instance Method Details

#persisted?Boolean

:nodoc

Returns:

  • (Boolean)

39
40
41
# File 'app/models/course/settings/sidebar.rb', line 39

def persisted? #:nodoc
  true
end

Read order from attributes and change the order of sidebar items.

Parameters:

  • attributes (Array<Hash>)

    the attributes which indicates the new order.


31
32
33
34
35
36
37
# File 'app/models/course/settings/sidebar.rb', line 31

def sidebar_items_attributes=(attributes)
  attributes.values.each do |attribute|
    key = attribute[:id]
    new_weight = attribute[:weight].to_i
    @settings.settings(key).weight = new_weight
  end
end

#update(attributes) ⇒ Object

Update settings with the hash attributes

Parameters:

  • attributes (Hash)

    The hash who stores the new settings


23
24
25
26
# File 'app/models/course/settings/sidebar.rb', line 23

def update(attributes)
  attributes.each { |k, v| send("#{k}=", v) }
  valid?
end

#valid?Boolean

:nodoc

Returns:

  • (Boolean)

43
44
45
# File 'app/models/course/settings/sidebar.rb', line 43

def valid? #:nodoc
  sidebar_items.all?(&:valid?)
end