refinery/refinerycms-page-images

Easy way to have page-images in my engine

prokopsimek opened this issue · 10 comments

Hi, I would like to have page-images in my generated engine... What is the best way to add page-images to my admin form?

My page-images.rb initializer:

Refinery::PageImages.configure do |config|
  # config.captions = false
  config.enable_for = [{:model=>"Refinery::Page", :tab=>"Refinery::Pages::Tab"}, {:model=>"Refinery::Stories::Story", :tab=>"Refinery::Stories::Story::Tab"}]
  # config.wysiwyg = true
end

My admin form:

<%= form_for [refinery, :stories_admin, @story] do |f| -%>
  <%= render '/refinery/admin/error_messages',
              :object => @story,
              :include_object_name => true %>

  <%= render '/refinery/admin/locale_picker',
              :current_locale => Globalize.locale %>
  <div class='field'>
    <%= f.label :title -%>
    <%= f.text_field :title, :class => 'larger widest' -%>
  </div>

  <div class='field'>
    <%= render '/refinery/admin/wysiwyg',
                :f => f,
                :fields => [:short_description, :long_description],
                :object => "stories/story" -%>
  </div>

  <%= render '/refinery/admin/form_actions', :f => f,
             :continue_editing => false,
             :delete_title => t('delete', :scope => 'refinery.stories.admin.stories.story'),
             :delete_confirmation => t('message', :scope => 'refinery.admin.delete', :title => @story.title) -%>
<% end -%>

<% content_for :javascripts do -%>
  <script>
    $(document).ready(function(){
      page_options.init(false, '', '');
    });
  </script>
<% end -%>

I would like to add it with nice way... Not like this e.g.:
https://github.com/refinery/refinerycms-blog/blob/master/app/views/refinery/blog/admin/posts/_form.html.erb#L23
or
https://github.com/refinery/refinerycms/blob/master/pages/app/views/refinery/admin/pages/_form_page_parts.html.erb#L33

Could I make it easier?
Like e.g.:

<%= render '/refinery/admin/wysiwyg',
                :f => f,
                :fields => [:short_description, :long_description, :page_images],
                :object => "stories/story" -%>

Thx

Unfortunately it's not a true wysiwyg field, so we invented the other APIs

And what is your recommended workflow to add page-images in custom engine?

Our workflow:

PageImages initializer:

 config.enable_for = [{:model=>"Refinery::Stories::Story", :tab=>"Refinery::Stories::Tab"}]

Model

has_many_page_images

Form:

   <div class='field'>
    <div id='page-tabs' class='clearfix ui-tabs ui-widget ui-widget-content ui-corner-all'>
      <ul id='page_parts'>
        <li class='ui-state-default ui-state-active'>
          <%= link_to t('content', :scope => 'activerecord.attributes.refinery/stories/story'), "#page_part_body" %>
        </li>
        <% Refinery::Stories.tabs.each_with_index do |tab, tab_index| %>
          <li class='ui-state-default' id="custom_<%= tab.name %>_tab">
            <%= link_to tab.name.titleize, "#custom_tab_#{tab_index}" %>
          </li>
        <% end %>
      </ul>

      <div id='page_part_editors'>
        <% part_index = -1 %>
          <%= render 'form_part', :f => f, :part_index => (part_index += 1) -%>
        <% Refinery::Stories.tabs.each_with_index do |tab, tab_index| %>
          <div class='page_part' id='<%= "custom_tab_#{tab_index}" %>'>
            <%= render tab.partial, :f => f %>
          </div>
        <% end %>
      </div>
    </div>
  </div>

/vendor/extensions/stories/lib/refinery/stories/tabs.rb

module Refinery
  module Stories    
    class Tab
      attr_accessor :name, :partial

      def self.register(&block)
        tab = self.new

        yield tab

        raise "A tab MUST have a name!: #{tab.inspect}" if tab.name.blank?
        raise "A tab MUST have a partial!: #{tab.inspect}" if tab.partial.blank?
      end

      protected

        def initialize
          ::Refinery::Stories.tabs << self # add me to the collection of registered page tabs
        end
    end
  end
end

/vendor/extensions/stories/lib/refinery/stories.rb

require 'refinerycms-core'

module Refinery
  autoload :StoriesGenerator, 'generators/refinery/stories_generator'

  module Stories
    require 'refinery/stories/engine'

    autoload :Tab, 'refinery/stories/tabs'

    class << self
      attr_writer :root
      attr_writer :tabs

      def root
        @root ||= Pathname.new(File.expand_path('../../../', __FILE__))
      end

      def tabs
        @tabs ||= []
      end

      def factory_paths
        @factory_paths ||= [ root.join('spec', 'factories').to_s ]
      end
    end
  end
end

Hello, where is "form_part" partial? %)

Maybe form_page_parts was meant?

thx!

This sure would be ideal:

    <%= render '/refinery/admin/wysiwyg',
            :f => f,
            :fields => [:description, :details, :page_images],
            :object => "products/product" -%>

And then just adding the initializer and has_many_page_images.

Ironically it was much easier in Refinery 1.0 to add page_images to engines.

No guide/solution for this?

In rails 4.2 need to add permit for write to database
/vendor/extensions/galleries/app/controllers/refinery/galleries/admin/galleries_controller.rb

params.require(:gallery).permit(:images_attributes => [:id, :caption, :image_page_id])