Class: JSAdmin::Resource
- Inherits:
-
Object
- Object
- JSAdmin::Resource
- Defined in:
- lib/js_admin/resource.rb
Instance Attribute Summary collapse
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
Instance Method Summary collapse
- #count ⇒ Object
- #display_name(record) ⇒ Object
- #editable_fields ⇒ Object
- #fields ⇒ Object
- #find(record_id) ⇒ Object
-
#initialize(model_class) ⇒ Resource
constructor
A new instance of Resource.
- #label ⇒ Object
- #list_fields ⇒ Object
- #model_id ⇒ Object
- #new_record(attributes = {}) ⇒ Object
- #next_page?(page) ⇒ Boolean
- #param_key ⇒ Object
- #previous_page?(page) ⇒ Boolean
- #records(page:) ⇒ Object
- #singular_label ⇒ Object
Constructor Details
#initialize(model_class) ⇒ Resource
Returns a new instance of Resource.
5 6 7 |
# File 'lib/js_admin/resource.rb', line 5 def initialize(model_class) @model_class = model_class end |
Instance Attribute Details
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
3 4 5 |
# File 'lib/js_admin/resource.rb', line 3 def model_class @model_class end |
Instance Method Details
#count ⇒ Object
49 50 51 |
# File 'lib/js_admin/resource.rb', line 49 def count model_class.count end |
#display_name(record) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/js_admin/resource.rb', line 61 def display_name(record) display_method = JSAdmin.configuration.display_name_methods.find do |method_name| record.respond_to?(method_name) && record.public_send(method_name).present? end return record.public_send(display_method).to_s if display_method "#{singular_label} ##{record.id}" end |
#editable_fields ⇒ Object
29 30 31 |
# File 'lib/js_admin/resource.rb', line 29 def editable_fields fields.select(&:editable?) end |
#fields ⇒ Object
25 26 27 |
# File 'lib/js_admin/resource.rb', line 25 def fields @fields ||= model_class.columns.map { |column| Field.new(self, column) } end |
#find(record_id) ⇒ Object
53 54 55 |
# File 'lib/js_admin/resource.rb', line 53 def find(record_id) model_class.find(record_id) end |
#label ⇒ Object
13 14 15 |
# File 'lib/js_admin/resource.rb', line 13 def label model_class.model_name.human(count: 2) end |
#list_fields ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/js_admin/resource.rb', line 33 def list_fields visible_fields = fields.reject(&:timestamp?) id_field = visible_fields.find(&:primary_key?) selected_fields = visible_fields.reject(&:primary_key?).first(6) [ id_field, *selected_fields ].compact end |
#model_id ⇒ Object
9 10 11 |
# File 'lib/js_admin/resource.rb', line 9 def model_id model_class.name.underscore.tr("/", "--") end |
#new_record(attributes = {}) ⇒ Object
57 58 59 |
# File 'lib/js_admin/resource.rb', line 57 def new_record(attributes = {}) model_class.new(attributes) end |
#next_page?(page) ⇒ Boolean
71 72 73 |
# File 'lib/js_admin/resource.rb', line 71 def next_page?(page) count > page.to_i * JSAdmin.configuration.records_per_page end |
#param_key ⇒ Object
21 22 23 |
# File 'lib/js_admin/resource.rb', line 21 def param_key model_class.model_name.param_key end |
#previous_page?(page) ⇒ Boolean
75 76 77 |
# File 'lib/js_admin/resource.rb', line 75 def previous_page?(page) page.to_i > 1 end |
#records(page:) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/js_admin/resource.rb', line 41 def records(page:) page_number = [ page.to_i, 1 ].max model_class.all .then { |scope| ordered_scope(scope) } .limit(JSAdmin.configuration.records_per_page) .offset((page_number - 1) * JSAdmin.configuration.records_per_page) end |
#singular_label ⇒ Object
17 18 19 |
# File 'lib/js_admin/resource.rb', line 17 def singular_label model_class.model_name.human end |