Class: RedmineExtensions::EasySettingPresenter

Inherits:
BasePresenter
  • Object
show all
Defined in:
app/presenters/redmine_extensions/easy_setting_presenter.rb

Defined Under Namespace

Classes: BooleanKeysAlsoToMapperKey

Instance Attribute Summary collapse

Attributes inherited from BasePresenter

#model, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasePresenter

#h, present, presenter_for, register, registered_presenters, #update_options

Constructor Details

#initialize(settings_params = {}, project = nil) ⇒ EasySettingPresenter

Returns a new instance of EasySettingPresenter.



10
11
12
13
14
15
16
17
# File 'app/presenters/redmine_extensions/easy_setting_presenter.rb', line 10

def initialize(settings_params={}, project = nil)
  ActiveSupport::Deprecation.warn('RedmineExtensions::EasySettingPresenter is deprecated in favor of EasySettings::ParamsWrapper.')

  @settings = settings_params || {}
  @settings = @settings.dup.symbolize_keys
  self.project = project
  super(EasySetting.new, nil)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *attrs) ⇒ Object



121
122
123
124
125
126
127
# File 'app/presenters/redmine_extensions/easy_setting_presenter.rb', line 121

def method_missing(meth, *attrs)
  if @plugin && @plugin.settings[:easy_settings] && @plugin.settings[:easy_settings].key?(meth.to_sym)
    EasySetting.value(prefix+meth.to_s, project_id)
  else
    super
  end
end

Instance Attribute Details

#pluginObject

Returns the value of attribute plugin.



4
5
6
# File 'app/presenters/redmine_extensions/easy_setting_presenter.rb', line 4

def plugin
  @plugin
end

#project_idObject

Returns the value of attribute project_id.



4
5
6
# File 'app/presenters/redmine_extensions/easy_setting_presenter.rb', line 4

def project_id
  @project_id
end

Class Method Details

.boolean_keysObject



6
7
8
# File 'app/presenters/redmine_extensions/easy_setting_presenter.rb', line 6

def self.boolean_keys
  @boolean_keys ||= BooleanKeysAlsoToMapperKey.new
end

Instance Method Details

#boolean_keysObject

TODO - more dynamic solution?



80
81
82
83
84
# File 'app/presenters/redmine_extensions/easy_setting_presenter.rb', line 80

def boolean_keys
  from_easy_setting = EasySetting.boolean_keys
  ActiveSupport::Deprecation.warn("EasySetting.boolean_keys has been deprecated, use EasySettingPresenter#boolean_keys instead") if from_easy_setting.any?
  from_easy_setting.concat(self.class.boolean_keys)
end

#easy_settingsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/presenters/redmine_extensions/easy_setting_presenter.rb', line 39

def easy_settings
  @easy_settings ||= @settings.collect do |name, value|
    # remove blank values in array settings
    value.delete_if{|v| v.blank? } if value.is_a?(Array)
    name = prefix+name.to_s

    set = EasySetting.where(name: name.to_s, project_id: project_id).first || EasySetting.new(name: name.to_s, project_id: project_id)

    set.value = format_value(name, value)

    set
  end
end

#format_value(name, value) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/presenters/redmine_extensions/easy_setting_presenter.rb', line 95

def format_value(name, value)
  case name.to_sym
  when *boolean_keys
    value.to_boolean
  when :attachment_description
    esa = EasySetting.where(:name => 'attachment_description_required', :project_id => nil).first
    case value
    when 'required'
      esa.update_attribute(:value, true)
      true
    when '1'
      esa.update_attribute(:value, false)
      true
    else
      esa.update_attribute(:value, false)
      false
    end
  when :agile_board_statuses
    value[:progress] = value.delete('progress') if value["progress"]
    value[:done] = value.delete('done') if value["done"]
    value
  else
    value
  end
end

#idObject



57
58
59
# File 'app/presenters/redmine_extensions/easy_setting_presenter.rb', line 57

def id
  @plugin && @plugin.id
end

#model_nameObject



69
70
71
# File 'app/presenters/redmine_extensions/easy_setting_presenter.rb', line 69

def model_name
  EasySetting.model_name
end

#param_keyObject



72
73
74
# File 'app/presenters/redmine_extensions/easy_setting_presenter.rb', line 72

def param_key
  EasySetting.param_key
end

#persisted?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'app/presenters/redmine_extensions/easy_setting_presenter.rb', line 53

def persisted?
  !!@plugin
end

#prefixObject



61
62
63
# File 'app/presenters/redmine_extensions/easy_setting_presenter.rb', line 61

def prefix
  @plugin && (@plugin.id.to_s + '_') || ''
end

#project=(project_or_project_id) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'app/presenters/redmine_extensions/easy_setting_presenter.rb', line 19

def project=(project_or_project_id)
  if project_or_project_id.is_a?(Project)
    @project_id = project_or_project_id.id
  elsif !project_or_project_id.nil?
    @project_id = project_or_project_id.to_i
  else
    @project_id = nil
  end
  @easy_settings = nil
end

#saveObject



86
87
88
89
90
91
92
93
# File 'app/presenters/redmine_extensions/easy_setting_presenter.rb', line 86

def save
  unsaved_settings.clear
  easy_settings.each do |setting|
    setting.save
    unsaved_settings << setting unless setting.persisted?
  end
  unsaved_settings.empty?
end

#to_keyObject



75
76
77
# File 'app/presenters/redmine_extensions/easy_setting_presenter.rb', line 75

def to_key
  @plugin && [@plugin.id]
end

#to_modelObject

TODO: form rendering methods. Maybe push them to the parent?



66
67
68
# File 'app/presenters/redmine_extensions/easy_setting_presenter.rb', line 66

def to_model
  self
end

#unsaved_settingsObject



35
36
37
# File 'app/presenters/redmine_extensions/easy_setting_presenter.rb', line 35

def unsaved_settings
  @unsaved_settings ||= []
end