---
# System prepended metadata

title: Refactor

---

### Refactor
```ruby!
  def new
    set_back_roster_url

    # Build template -> service || Form builder
    message_templates = @school.message_templates
    message_templates = message_templates.with_internal if params[:recipient] == 'internal'
    @template = message_templates.new(filters: {})
    @template.kind = MessageTemplate.target_to_kind.fetch(params[:target], MessageTemplate::GENERAL_TEMPLATE)

    # Get filtered templates -> Model
    @templates = filter_templates(@templates, unlayer: params[:unlayer], template: @template, recipient: params[:recipient], target: params[:target])
    sort_folders_with_templates(@templates)

    # additional settings -> private (current_user related to view layer)
    @applicants = GetMessageTemplateApplicantsService.new(@template, @school, current_user).perform
    @parents_will_notified = load_parents_will_notified(@template, @applicants)
    @agents_will_notified = load_agents_will_notified(@template, @applicants)

    # Build template & set template -> service
    if (from_template_id = params['from_message_template_id'])
      from_template = @school.message_templates.find_by(id: from_template_id)
      if from_template
        @template.subject = from_template.subject
        @template.body = from_template.body
      end
    end
    render action: :new, layout: 'nv_admin'
  end
```

#### Part I - service(Form Builder)
```ruby!
# app/services/message_template_creator_service.rb

class MessageTemplateCreatorService
  def initialize(school, params, current_user)
    @school = school
    @params = params
    @current_user = current_user
  end

  def create_template
    message_templates = @school.message_templates
    message_templates = message_templates.with_internal if @params[:recipient] == 'internal'
    template = message_templates.new(filters: {})
    template.kind = MessageTemplate.target_to_kind.fetch(@params[:target], MessageTemplate::GENERAL_TEMPLATE)
    
    if (from_template_id = @params['from_message_template_id'])
      from_template = @school.message_templates.find_by(id: from_template_id)
      if from_template
        template.subject = from_template.subject
        template.body = from_template.body
      end
    end
    
    template
  end
end

```

#### Part II - Model
```ruby!
# app/models/message_template.rb

class MessageTemplate < ApplicationRecord

  def self.filter_and_sort_templates(params, school)
    templates = school.message_templates
    templates = templates.with_internal if params[:recipient] == 'internal'
    
    
    templates
  end
end

private

def load_objects
    query_string = 'name != "' + MessageTemplate::SYSTEM_LEVEL_TEMPLATE_NAMES.join('" AND name != "') + '" OR name IS NULL'
    @templates = @school.message_templates.where(query_string)

    gon.keywords = MessageTemplate::VARIABLES
 end
```

#### Part III - private
```ruby!
private

def setup_applicants
  @applicants = GetMessageTemplateApplicantsService.new(@template, @school, current_user).perform
  @parents_will_notified = load_parents_will_notified(@template, @applicants)
  @agents_will_notified = load_agents_will_notified(@template, @applicants)
end
```

#### Result
```ruby!
def new
  set_back_roster_url
  @template = MessageTemplateCreatorService.new(@school, params, current_user).create_template
  @templates = MessageTemplate.filter_and_sort_templates(params, @school)
  setup_applicants
  render action: :new, layout: 'nv_admin'
end
```