Module: Basepack

Defined in:
lib/basepack/model.rb,
lib/basepack.rb,
lib/basepack/utils.rb,
lib/basepack/engine.rb,
lib/basepack/version.rb,
lib/basepack/filter_ql.rb,
lib/basepack/delegation.rb,
lib/basepack/forms/base.rb,
lib/basepack/forms/diff.rb,
lib/basepack/forms/edit.rb,
lib/basepack/forms/list.rb,
lib/basepack/forms/show.rb,
lib/basepack/renderable.rb,
lib/basepack/forms/query.rb,
lib/basepack/forms/export.rb,
lib/basepack/forms/import.rb,
lib/basepack/import/actions.rb,
app/models/basepack/settings.rb,
lib/basepack/forms/bulk_edit.rb,
lib/basepack/forms/fields/base.rb,
lib/basepack/forms/groups/base.rb,
lib/basepack/forms/groups/diff.rb,
lib/basepack/import/controller.rb,
lib/basepack/import/importable.rb,
lib/basepack/forms/factories/base.rb,
lib/basepack/import/model_dragonfly.rb,
app/models/basepack/import_importable.rb,
lib/basepack/forms/fields/color_picker.rb,
app/controllers/basepack/base_controller.rb,
lib/basepack/forms/factories/rails_admin.rb,
lib/generators/basepack/install_generator.rb,
lib/basepack/forms/fields/single_association.rb,
lib/basepack/forms/fields/has_one_association.rb,
lib/basepack/forms/factories/query_rails_admin.rb,
lib/basepack/forms/fields/has_many_association.rb,
lib/basepack/forms/fields/belongs_to_association.rb,
lib/generators/basepack/scaffold/scaffold_generator.rb,
lib/basepack/forms/fields/has_and_belongs_to_many_association.rb

Overview

This class construct form for filtering an resource For playing in console, you can instantiate by: Basepack::Forms::Factories::QueryRailsAdmin.new(helper).new_form(Subscription, Subscription.all, auth_object: Ability.new(User.first))

Defined Under Namespace

Modules: Delegation, Forms, Generators, Import, Model, Renderable, Utils Classes: BaseController, Engine, FilterQL, ImportImportable, Railtie, Settings

Constant Summary collapse

VERSION =
"1.0.1".freeze

Class Method Summary collapse

Class Method Details

.map_value_csv(value) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
# File 'lib/basepack.rb', line 225

def self.map_value_csv(value)
  case value
  when Hash
    value.map {|k, v| map_value_csv(v) }.flatten
  when Array
    arr = value.map {|v| Array.wrap(map_value_csv(v)) }
    arr.empty? ? arr : arr.first.zip(*arr[1..-1]).map {|a| a.join(',')}
  else
    value
  end
end

.setupObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/basepack.rb', line 11

def self.setup

  Array.class_eval do
    def to_csv(schema = {}, options = {})
      if all? {|o| o.respond_to?(:to_csv)}
        map {|o| o.to_csv(schema, options) }.join
      else
        CSV.generate_line(self, options.reverse_merge(encoding: 'UTF-8'))
      end
    end
  end

  ActiveRecord::Base.class_eval do
    def to_label
      send(Basepack::Settings.to_label_methods.find {|m| respond_to?(m)} || :to_s)
    end

    def to_details_label
      @view ||= begin
        view = ActionView::Base.new(ActionController::Base.view_paths, {})
        view.extend ApplicationHelper
        view
      end
      begin
        @view.render(partial: "#{self.class.model_name.route_key}/details_label", layout: false, locals: { resource: self } ).html_safe
      rescue ActionView::MissingTemplate
        to_label
      end
    end

    def labelize(&block)
      if new_record?
        ""
      else
        yield.to_s.truncate(40, :separator => ' ')
      end
    end

    def to_csv(schema = {}, options = {})
      schema = { include_root_in_json: false }.reverse_merge!(schema)
      values = Basepack.map_value_csv(as_json(schema))
      CSV.generate_line(values, options.reverse_merge(encoding: 'UTF-8'))
    end

    #def self.ransackable_attributes(auth_object = nil)
    #  authorizer = active_authorizer[:default]
    #  super(auth_object).reject {|a| a != 'id' && authorizer.deny?(a)}
    #end

    #def self.ransackable_associations(auth_object = nil)
    #  authorizer = active_authorizer[:default]
    #  super(auth_object).reject {|a| authorizer.deny?("#{a}_id") && authorizer.deny?("#{a}_attributes")}
    #end
  end

  ActionController::Renderers.add :csv do |obj, options|
    filename = options[:filename] || 'data'
    str = obj.respond_to?(:to_csv) ? obj.to_csv : obj.to_s
    send_data str, :type => Mime::CSV, :disposition => "attachment; filename=#{filename}.csv"
  end

  if defined? Ransack
    Ransack::Search.class_eval do
      def errors
        @errors_patch ||= {}
      end

      attr_accessor :custom_filters
    end
  end

  if defined? RailsAdmin

    RailsAdmin::Config::Sections::List.class_eval do
      register_instance_option :partial do
        'forms/list'
      end
    end

    RailsAdmin::Config::Fields::Association.class_eval do
      alias pretty_value! pretty_value

      register_instance_option :pretty_value do
        if RailsAdmin::ApplicationController === bindings[:controller]
          pretty_value!
        else
          bindings[:view].form_field_show_association(self)
        end
      end

      register_instance_option :options_source do
        begin
          bindings[:view].url_for([:options, associated_model_config.abstract_model.model])
        rescue
            Rails.logger.debug <<-MESSAGE.strip_heredoc


            [Basepack] Please add routes for '#{associated_model_config.abstract_model.model}' - url_for([:options, #{associated_model_config.abstract_model.model}]) failed

            MESSAGE
          "/#{associated_model_config.abstract_model.model.name.parameterize}-options-url-not-exists"
        end
      end

      register_instance_option :options_source_params do
        {}
      end

      register_instance_option :precached_options do
        nil
      end

      register_instance_option :partial_show do
        nil
      end
    end

    RailsAdmin::Config::Sections::List.class_eval do
      register_instance_option :bulk_actions do
        false
      end
    end

    RailsAdmin::Config::Fields::Base.class_eval do
      alias export_value! export_value

      register_instance_option :export_value do
        formatted_value
      end
    end

    RailsAdmin::Config::Fields::Types::Datetime.class_eval do
      alias export_value! export_value

      register_instance_option :export_value do
        unless (time = value).nil?
          I18n.l(time, format: "%d.%m.%Y %T")
        else
          ""
        end
      end

      register_instance_option :bulk_edit_partial do
        nil
      end
    end

    RailsAdmin::Config::Fields::Types::Time.class_eval do
      register_instance_option :export_value do
        export_value!
      end
    end

    RailsAdmin::Config::Fields::Types::Text.class_eval do
      register_instance_option :show_with_pre do
        true
      end

      register_instance_option :pretty_value do
        if show_with_pre and formatted_value.presence
          bindings[:view]. :pre, formatted_value
        else
          formatted_value
        end
      end
    end

    #RailsAdmin::Config::Fields::Types::Enum.class_eval do
    #  register_instance_option :pretty_value do
    #    v = bindings[:object].send(name)
    #    v ? v.text : ' - '
    #  end
    #end

    #RailsAdmin::Adapters::ActiveRecord.class_eval do
    #  private

    #  alias build_statement! build_statement

    #  def build_statement(column, type, value, operator)
    #    result = build_statement!(column, type, value, operator)
    #    return unless result.present?

    #    case type
    #    when :enum
    #      if operator == '_blank' || value == '_blank'
    #        result = ["(#{column} IS NULL)"]
    #      elsif operator == '_present' || value == '_present'
    #        result = ["(#{column} IS NOT NULL)"]
    #      else
    #        in_values = {}
    #
    #        if enum_attrs = model.enumerized_attributes[column[/([^.]+)$/, 1].to_sym]
    #          enum_values = enum_attrs.values

    #          values = Array.wrap(value).each do |val|
    #            enum_values.each do |e_val| 
    #              if e_val == val or e_val.value == val or e_val.text.downcase.include?(val.downcase)
    #                in_values[e_val.value] = true
    #              end
    #            end
    #          end
    #        end

    #        result = in_values.empty? ? nil : ["(#{column} IN (?))", in_values.keys]
    #      end
    #    end

    #    return result
    #  end
    #end
  end
end