Skip to content

Commit

Permalink
🔧 Limit snippet.description.length, #136
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoshizaki committed Jun 7, 2018
1 parent d38249d commit 6400491
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 13 deletions.
1 change: 1 addition & 0 deletions app/controllers/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ def ajax_create_snippet
@lesson = Lesson.find_by(course_id: @course.id, content_id: @content.id)
note = @course.lesson_note(session[:id])
display_order = note.note_indices.size + 1

Snippet.transaction do
snippet = Snippet.create!(manager_id: session[:id], category: 'text', description: params[:description], source_type: 'page', source_id: @content.page_id(session[:page_num]))
NoteIndex.create!(note_id: note.id, item_id: snippet.id, item_type: 'Snippet', display_order: display_order)
Expand Down
2 changes: 0 additions & 2 deletions app/controllers/snippets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ def ajax_update
render_snippets params[:note_id].to_i
end
else
# max character length for user text form is USER_TEXT_LENGTH
params[:snippet][:description] = params[:snippet][:description][0, USER_TEXT_LENGTH]
snippet.update_attributes(snippet_params)

if %w[header subheader].include? snippet.category
Expand Down
7 changes: 7 additions & 0 deletions app/models/snippet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Snippet < ApplicationRecord
validates_inclusion_of :source_type, in: %w[direct page upload web]
validates_format_of :description, with: /\.(gif|jpe?g|png)/i, message: 'must have an image extension', if: "source_type == 'web' && category == 'image'"
after_destroy :destroy_source
before_save :limit_description_length

# ====================================================================
# Public Functions
Expand Down Expand Up @@ -92,4 +93,10 @@ def destroy_source
source.destroy if source.deletable?
end
end

def limit_description_length
# max character length for snippet is SNIPPET_MAX_LENGTH
# description is image url for source_type == web and category == image
self.description = description[0, SNIPPET_MAX_LENGTH] unless category == "image"
end
end
4 changes: 2 additions & 2 deletions app/views/snippets/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
<script>
$(function(){
$('#<%= form_id %> textarea').bind('keydown keyup change',function () {
charCount("<%= form_id %>", $(this).val().length, <%= USER_TEXT_LENGTH %>);
charCount("<%= form_id %>", $(this).val().length, <%= SNIPPET_MAX_LENGTH %>);
});
charCount("<%= form_id %>", $('#<%= form_id %> textarea').val().length, <%= USER_TEXT_LENGTH %>);
charCount("<%= form_id %>", $('#<%= form_id %> textarea').val().length, <%= SNIPPET_MAX_LENGTH %>);
});
</script>
<% end %>
8 changes: 4 additions & 4 deletions config/initializers/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
# max number for assignment outcome file per assignment
OUTCOME_MAX_SIZE = 9

# max character length for snippet
SNIPPET_MAX_LENGTH = 800

# min and max character length for user password
USER_PASSWORD_MIN_LENGTH = 6
USER_PASSWORD_MAX_LENGTH = 14
Expand All @@ -76,13 +79,10 @@
# max number of user search results for system administrator
USER_SEARCH_MAX_SIZE = 50

# max character length for user text form
USER_TEXT_LENGTH = 800

# character length for user token
USER_TOKEN_LENGTH = 20

# max character length for web snippet
# max character length for web snippet (less or equal to SNIPPET_MAX_LENGTH)
WEB_SNIPPET_MAX_LENGTH = 300

# FIXME: PushNotification
Expand Down
4 changes: 2 additions & 2 deletions config/locales/views/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ en:
group: :course_members.index.group
home:
export: Export to text
reload: Reload note
reload: Reload

outcomes:
evaluator:
Expand Down Expand Up @@ -302,7 +302,7 @@ en:
create_snippet_btns:
header: Header
subheader: Subheader
text: Text
text: Paragraph
form:
cancel: :layouts.buttons.cancel.cancel
char_num_error: :notes.form.char_num_error
Expand Down
6 changes: 3 additions & 3 deletions config/locales/views/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ ja:
courses:
group: :course_members.index.group
home:
export: 文章を出力
reload: ノートを再読み込み
export: テキスト出力
reload: 再読み込み

outcomes:
evaluator:
Expand Down Expand Up @@ -302,7 +302,7 @@ ja:
create_snippet_btns:
header: 見出し
subheader: 小見出し
text: 文章
text: 段落
form:
cancel: :layouts.buttons.cancel.cancel
char_num_error: :notes.form.char_num_error
Expand Down

0 comments on commit 6400491

Please sign in to comment.