Feature #1767 » 0004-Make-spent-time-project-custom-fields-configurable-s.patch
| app/controllers/custom_fields_controller.rb | ||
|---|---|---|
|
format.html do
|
||
|
@custom_fields_by_type = CustomField.all.group_by {|f| f.class.name}
|
||
|
@custom_fields_projects_count =
|
||
|
IssueCustomField.where(is_for_all: false).joins(:projects).group(:custom_field_id).count
|
||
|
[IssueCustomField, TimeEntryCustomField, ProjectCustomField].each_with_object({}) do |klass, _|
|
||
|
_.merge!(
|
||
|
klass.where(is_for_all: false).joins(:projects).group(:custom_field_id).count
|
||
|
)
|
||
|
end
|
||
|
end
|
||
|
format.api do
|
||
|
@custom_fields = CustomField.all
|
||
| app/controllers/project_enumerations_controller.rb | ||
|---|---|---|
|
before_action :authorize
|
||
|
def update
|
||
|
if @project.update_or_create_time_entry_activities(update_params)
|
||
|
flash[:notice] = l(:notice_successful_update)
|
||
|
Project.transaction do
|
||
|
if params[:project]
|
||
|
@project.safe_attributes = params[:project]
|
||
|
raise ActiveRecord::Rollback unless @project.save
|
||
|
end
|
||
|
if @project.update_or_create_time_entry_activities(update_params)
|
||
|
flash[:notice] = l(:notice_successful_update)
|
||
|
else
|
||
|
raise ActiveRecord::Rollback
|
||
|
end
|
||
|
end
|
||
|
redirect_to settings_project_path(@project, :tab => 'activities')
|
||
| app/controllers/projects_controller.rb | ||
|---|---|---|
|
def new
|
||
|
@issue_custom_fields = IssueCustomField.sorted.to_a
|
||
|
@project_custom_fields = ProjectCustomField.sorted.to_a
|
||
|
@trackers = Tracker.sorted.to_a
|
||
|
@project = Project.new
|
||
|
@project.safe_attributes = params[:project]
|
||
| ... | ... | |
|
def create
|
||
|
@issue_custom_fields = IssueCustomField.sorted.to_a
|
||
|
@project_custom_fields = ProjectCustomField.sorted.to_a
|
||
|
@trackers = Tracker.sorted.to_a
|
||
|
@project = Project.new
|
||
|
@project.safe_attributes = params[:project]
|
||
| ... | ... | |
|
def copy
|
||
|
@issue_custom_fields = IssueCustomField.sorted.to_a
|
||
|
@project_custom_fields = ProjectCustomField.sorted.to_a
|
||
|
@time_entry_custom_fields = TimeEntryCustomField.sorted.to_a
|
||
|
@trackers = Tracker.sorted.to_a
|
||
|
@source_project = Project.find(params[:id])
|
||
|
if request.get?
|
||
| ... | ... | |
|
def settings
|
||
|
@issue_custom_fields = IssueCustomField.sorted.to_a
|
||
|
@project_custom_fields = ProjectCustomField.sorted.to_a
|
||
|
@time_entry_custom_fields = TimeEntryCustomField.sorted.to_a
|
||
|
@issue_category ||= IssueCategory.new
|
||
|
@member ||= @project.members.new
|
||
|
@trackers = Tracker.sorted.to_a
|
||
| app/models/custom_field.rb | ||
|---|---|---|
|
end
|
||
|
if self.is_a?(IssueCustomField)
|
||
|
self.tracker_ids = custom_field.tracker_ids.dup
|
||
|
end
|
||
|
if %w(IssueCustomField TimeEntryCustomField ProjectCustomField).include?(self.class.name)
|
||
|
self.project_ids = custom_field.project_ids.dup
|
||
|
end
|
||
|
self
|
||
| app/models/project.rb | ||
|---|---|---|
|
:class_name => 'IssueCustomField',
|
||
|
:join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
|
||
|
:association_foreign_key => 'custom_field_id'
|
||
|
has_and_belongs_to_many :project_custom_fields,
|
||
|
lambda {order(:position)},
|
||
|
:class_name => 'ProjectCustomField',
|
||
|
:join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
|
||
|
:association_foreign_key => 'custom_field_id'
|
||
|
has_and_belongs_to_many :time_entry_custom_fields,
|
||
|
lambda {order(:position)},
|
||
|
:class_name => 'TimeEntryCustomField',
|
||
|
:join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
|
||
|
:association_foreign_key => 'custom_field_id'
|
||
|
# Default Custom Query
|
||
|
belongs_to :default_issue_query, :class_name => 'IssueQuery'
|
||
| ... | ... | |
|
@rolled_up_statuses = nil
|
||
|
@rolled_up_custom_fields = nil
|
||
|
@all_issue_custom_fields = nil
|
||
|
@all_project_custom_fields = nil
|
||
|
@all_time_entry_custom_fields = nil
|
||
|
@to_param = nil
|
||
|
@allowed_parents = nil
|
||
| ... | ... | |
|
end
|
||
|
end
|
||
|
# Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
|
||
|
def available_custom_fields
|
||
|
all_project_custom_fields
|
||
|
end
|
||
|
def all_project_custom_fields
|
||
|
@all_project_custom_fields ||=
|
||
|
if new_record?
|
||
|
ProjectCustomField.sorted.
|
||
|
where("is_for_all = ? OR id IN (?)", true, project_custom_field_ids)
|
||
|
else
|
||
|
ProjectCustomField.sorted.
|
||
|
where("is_for_all = ? OR id IN (SELECT DISTINCT cfp.custom_field_id" +
|
||
|
" FROM #{table_name_prefix}custom_fields_projects#{table_name_suffix} cfp" +
|
||
|
" WHERE cfp.project_id = ?)", true, id)
|
||
|
end
|
||
|
end
|
||
|
def all_time_entry_custom_fields
|
||
|
@all_time_entry_custom_fields ||=
|
||
|
if new_record?
|
||
|
TimeEntryCustomField.sorted.
|
||
|
where("is_for_all = ? OR id IN (?)", true, time_entry_custom_field_ids)
|
||
|
else
|
||
|
TimeEntryCustomField.sorted.
|
||
|
where("is_for_all = ? OR id IN (SELECT DISTINCT cfp.custom_field_id" +
|
||
|
" FROM #{table_name_prefix}custom_fields_projects#{table_name_suffix} cfp" +
|
||
|
" WHERE cfp.project_id = ?)", true, id)
|
||
|
end
|
||
|
end
|
||
|
# Returns a scope of all custom fields enabled for issues of the project
|
||
|
# and its subprojects
|
||
|
def rolled_up_custom_fields
|
||
| ... | ... | |
|
'custom_fields',
|
||
|
'tracker_ids',
|
||
|
'issue_custom_field_ids',
|
||
|
'project_custom_field_ids',
|
||
|
'time_entry_custom_field_ids',
|
||
|
'parent_id',
|
||
|
'default_version_id',
|
||
|
'default_issue_query_id',
|
||
| ... | ... | |
|
end
|
||
|
end
|
||
|
if project_custom_field_ids = attrs.delete('project_custom_field_ids')
|
||
|
super({'project_custom_field_ids' => project_custom_field_ids}, user)
|
||
|
end
|
||
|
# Reject custom fields values not visible by the user
|
||
|
if attrs['custom_field_values'].present?
|
||
|
editable_custom_field_ids = editable_custom_field_values(user).map {|v| v.custom_field_id.to_s}
|
||
| ... | ... | |
|
copy.trackers = project.trackers
|
||
|
copy.custom_values = project.custom_values.collect {|v| v.clone}
|
||
|
copy.issue_custom_fields = project.issue_custom_fields
|
||
|
copy.project_custom_fields = project.project_custom_fields
|
||
|
copy.time_entry_custom_fields = project.time_entry_custom_fields
|
||
|
copy
|
||
|
end
|
||
| app/models/project_custom_field.rb | ||
|---|---|---|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
|
class ProjectCustomField < CustomField
|
||
|
has_and_belongs_to_many :projects, :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}", :foreign_key => "custom_field_id", :autosave => true
|
||
|
safe_attributes 'project_ids'
|
||
|
def type_name
|
||
|
:label_project_plural
|
||
|
end
|
||
| ... | ... | |
|
def visibility_by_project_condition(project_key=nil, user=User.current, id_column=nil)
|
||
|
project_key ||= "#{Project.table_name}.id"
|
||
|
super(project_key, user, id_column)
|
||
|
id_column ||= id
|
||
|
sql = super(project_key, user, id_column)
|
||
|
project_condition = "EXISTS (SELECT 1 FROM #{CustomField.table_name} ifa WHERE ifa.is_for_all = #{self.class.connection.quoted_true} AND ifa.id = #{id_column})" +
|
||
|
" OR #{Project.table_name}.id IN (SELECT project_id FROM #{table_name_prefix}custom_fields_projects#{table_name_suffix} WHERE custom_field_id = #{id_column})"
|
||
|
"((#{sql}) AND (#{project_condition}))"
|
||
|
end
|
||
|
end
|
||
| app/models/time_entry.rb | ||
|---|---|---|
|
end
|
||
|
end
|
||
|
# Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
|
||
|
def available_custom_fields
|
||
|
p = issue&.project || project
|
||
|
p ? p.all_time_entry_custom_fields : super
|
||
|
end
|
||
|
def assignable_users
|
||
|
users = []
|
||
|
if project
|
||
| app/models/time_entry_custom_field.rb | ||
|---|---|---|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
|
class TimeEntryCustomField < CustomField
|
||
|
has_and_belongs_to_many :projects, :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}", :foreign_key => "custom_field_id", :autosave => true
|
||
|
safe_attributes 'project_ids'
|
||
|
def type_name
|
||
|
:label_spent_time
|
||
|
end
|
||
| ... | ... | |
|
super || (roles & user.roles_for_project(project)).present?
|
||
|
end
|
||
|
def visibility_by_project_condition(project_key=nil, user=User.current, id_column=nil)
|
||
|
id_column ||= id
|
||
|
sql = super(project_key, user, id_column)
|
||
|
project_condition = "EXISTS (SELECT 1 FROM #{CustomField.table_name} ifa WHERE ifa.is_for_all = #{self.class.connection.quoted_true} AND ifa.id = #{id_column})" +
|
||
|
" OR #{TimeEntry.table_name}.project_id IN (SELECT project_id FROM #{table_name_prefix}custom_fields_projects#{table_name_suffix} WHERE custom_field_id = #{id_column})"
|
||
|
"((#{sql}) AND (#{project_condition}))"
|
||
|
end
|
||
|
def validate_custom_field
|
||
|
super
|
||
|
errors.add(:base, l(:label_role_plural) + ' ' + l('activerecord.errors.messages.blank')) unless visible? || roles.present?
|
||
| app/views/custom_fields/_form.html.erb | ||
|---|---|---|
|
<% if @custom_field.is_a?(IssueCustomField) %>
|
||
|
<%= render :partial => 'visibility_by_tracker_selector', :locals => { :f => f } %>
|
||
|
<% end %>
|
||
|
<% if %w(IssueCustomField TimeEntryCustomField ProjectCustomField).include?(@custom_field.class.name) %>
|
||
|
<%= render :partial => 'visibility_by_project_selector', :locals => { :f => f } %>
|
||
|
<% end %>
|
||
|
</div>
|
||
| app/views/custom_fields/_index.html.erb | ||
|---|---|---|
|
<th><%=l(:field_name)%></th>
|
||
|
<th><%=l(:field_field_format)%></th>
|
||
|
<th><%=l(:field_is_required)%></th>
|
||
|
<% if tab[:name] == 'IssueCustomField' %>
|
||
|
<% if %w(IssueCustomField TimeEntryCustomField ProjectCustomField).include?(tab[:name]) %>
|
||
|
<th><%=l(:field_is_for_all)%></th>
|
||
|
<th><%=l(:label_used_by)%></th>
|
||
|
<% end %>
|
||
| ... | ... | |
|
<td class="name"><%= link_to custom_field.name, edit_custom_field_path(custom_field) %></td>
|
||
|
<td><%= l(custom_field.format.label) %></td>
|
||
|
<td><%= checked_image custom_field.is_required? %></td>
|
||
|
<% if tab[:name] == 'IssueCustomField' %>
|
||
|
<% if %w(IssueCustomField TimeEntryCustomField ProjectCustomField).include?(tab[:name]) %>
|
||
|
<td><%= checked_image custom_field.is_for_all? %></td>
|
||
|
<td><%= l(:label_x_projects, :count => @custom_fields_projects_count[custom_field.id] || 0) if custom_field.is_a? IssueCustomField and !custom_field.is_for_all? %></td>
|
||
|
<td><%= l(:label_x_projects, :count => @custom_fields_projects_count[custom_field.id] || 0) if [IssueCustomField, TimeEntryCustomField, ProjectCustomField].include?(custom_field.class) and !custom_field.is_for_all? %></td>
|
||
|
<% end %>
|
||
|
<td class="buttons">
|
||
|
<%= reorder_handle(custom_field, :url => custom_field_path(custom_field), :param => 'custom_field') %>
|
||
| app/views/projects/_form.html.erb | ||
|---|---|---|
|
<%= call_hook(:view_projects_form, :project => @project, :form => f) %>
|
||
|
</div>
|
||
|
<% unless @project_custom_fields.empty? %>
|
||
|
<fieldset class="box tabular" id="project_project_custom_fields"><legend><%= toggle_checkboxes_link('#project_project_custom_fields input[type=checkbox]:enabled') %><%=l(:label_custom_field_plural)%></legend>
|
||
|
<% if User.current.admin? %>
|
||
|
<div class="contextual"><%= link_to l(:label_administration), custom_fields_path(tab: ProjectCustomField.name), class: "icon icon-settings" %></div>
|
||
|
<% end %>
|
||
|
<% all_project_custom_fields = @project.all_project_custom_fields %>
|
||
|
<% @project_custom_fields.each do |custom_field| %>
|
||
|
<label class="floating">
|
||
|
<%= check_box_tag 'project[project_custom_field_ids][]', custom_field.id, all_project_custom_fields.include?(custom_field),
|
||
|
disabled: (custom_field.is_for_all? ? "disabled" : nil),
|
||
|
id: nil %>
|
||
|
<%= custom_field_name_tag(custom_field) %>
|
||
|
</label>
|
||
|
<% end %>
|
||
|
<%= hidden_field_tag 'project[project_custom_field_ids][]', '', id: nil %>
|
||
|
</fieldset>
|
||
|
<%= javascript_tag do %>
|
||
|
$(document).ready(function(){
|
||
|
var custom_field_toggle_disabled = function(custom_field_id){
|
||
|
var custom_field_value = $('#project_custom_field_values_' + $(custom_field_id).attr('value'));
|
||
|
custom_field_value.prop('disabled', !$(custom_field_id).prop('checked'));
|
||
|
};
|
||
|
$('#project_project_custom_fields input[type=checkbox]').change(function(){
|
||
|
custom_field_toggle_disabled(this);
|
||
|
});
|
||
|
$('#project_project_custom_fields input[type=checkbox]').each(function(){
|
||
|
custom_field_toggle_disabled(this);
|
||
|
});
|
||
|
});
|
||
|
<% end %>
|
||
|
<% end %>
|
||
|
<% if @project.safe_attribute?('enabled_module_names') %>
|
||
|
<fieldset class="box tabular" id="project_modules"><legend><%= toggle_checkboxes_link('#project_modules input[type="checkbox"]') %><%= l(:label_module_plural) %></legend>
|
||
|
<% Redmine::AccessControl.available_project_modules.each do |m| %>
|
||
| app/views/projects/copy.html.erb | ||
|---|---|---|
|
<%= hidden_field_tag 'project[issue_custom_field_ids][]', issue_custom_field_id %>
|
||
|
<% end %>
|
||
|
<% @project.time_entry_custom_field_ids.each do |time_entry_custom_field_id| %>
|
||
|
<%= hidden_field_tag 'project[time_entry_custom_field_ids][]', time_entry_custom_field_id %>
|
||
|
<% end %>
|
||
|
<%= submit_tag l(:button_copy) %>
|
||
|
<% end %>
|
||
| app/views/projects/settings/_activities.html.erb | ||
|---|---|---|
|
<% end %>
|
||
|
</table>
|
||
|
<% unless @time_entry_custom_fields.empty? %>
|
||
|
<fieldset class="box tabular" id="project_time_entry_custom_fields"><legend><%= toggle_checkboxes_link('#project_time_entry_custom_fields input[type=checkbox]:enabled') %><%=l(:label_custom_field_plural)%></legend>
|
||
|
<% if User.current.admin? %>
|
||
|
<div class="contextual"><%= link_to l(:label_administration), custom_fields_path(tab: TimeEntryCustomField.name), class: "icon icon-settings" %></div>
|
||
|
<% end %>
|
||
|
<% all_time_entry_custom_fields = @project.all_time_entry_custom_fields %>
|
||
|
<% @time_entry_custom_fields.each do |custom_field| %>
|
||
|
<label class="floating">
|
||
|
<%= check_box_tag 'project[time_entry_custom_field_ids][]', custom_field.id, all_time_entry_custom_fields.include?(custom_field),
|
||
|
disabled: (custom_field.is_for_all? ? "disabled" : nil),
|
||
|
id: nil %>
|
||
|
<%= custom_field_name_tag(custom_field) %>
|
||
|
</label>
|
||
|
<% end %>
|
||
|
<%= hidden_field_tag 'project[time_entry_custom_field_ids][]', '', id: nil %>
|
||
|
</fieldset>
|
||
|
<% end %>
|
||
|
<%= submit_tag l(:button_save) %>
|
||
|
<% end %>
|
||